2014-04-21 16:13:41 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2014-04-24 15:24:38 +02:00
|
|
|
import re, sys
|
2014-04-21 16:13:41 +02:00
|
|
|
from lxml import etree
|
|
|
|
|
|
|
|
|
|
|
|
# Take an English strings.xml file and another, "join" them on the
|
|
|
|
# name of each string, and then produce an array that's a mapping of
|
|
|
|
# English to the other. Get ride of extra whitespace etc in the
|
|
|
|
# English strings so they're identical to how an Android app displays
|
|
|
|
# them.
|
|
|
|
|
|
|
|
english = 'res/values/strings.xml'
|
|
|
|
other_f = 'res_src/values-%s/strings.xml'
|
|
|
|
|
2014-04-24 15:24:38 +02:00
|
|
|
def readIDs(base):
|
|
|
|
ids = {}
|
|
|
|
start = re.compile('\s*public static final class string {\s*')
|
|
|
|
end = re.compile('\s*}\s*')
|
|
|
|
entry = re.compile('\s*public static final int (\S+)=(0x.*);\s*')
|
|
|
|
inLine = False
|
|
|
|
path = base + '/archive/R.java'
|
|
|
|
for line in open(path, 'r'):
|
|
|
|
line = line.strip()
|
|
|
|
# print line
|
|
|
|
if inLine:
|
|
|
|
if end.match(line):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
match = entry.match(line)
|
|
|
|
if match:
|
|
|
|
name = match.group(1)
|
|
|
|
value = int(match.group(2), 16)
|
|
|
|
ids[name] = value
|
|
|
|
elif start.match(line):
|
|
|
|
inLine = True
|
|
|
|
return ids
|
|
|
|
|
|
|
|
def asMap( path, ids ):
|
2014-04-21 16:13:41 +02:00
|
|
|
map = {}
|
|
|
|
parser = etree.XMLParser(remove_blank_text=True)
|
|
|
|
doc = etree.parse( path, parser )
|
|
|
|
for elem in doc.getroot().iter():
|
|
|
|
if 'string' == elem.tag:
|
|
|
|
text = elem.text
|
|
|
|
if text:
|
2014-04-24 04:35:35 +02:00
|
|
|
# print 'text before:', text
|
|
|
|
text = " ".join(re.split('\s+', text)) \
|
|
|
|
.replace("\\'", "'") \
|
|
|
|
.replace( '\\"', '"' )
|
|
|
|
# print 'text after:', text
|
2014-04-24 15:24:38 +02:00
|
|
|
name = elem.get('name')
|
|
|
|
id = ids[name]
|
|
|
|
map[id] = text
|
2014-04-21 16:13:41 +02:00
|
|
|
return map
|
|
|
|
|
2014-04-24 04:35:35 +02:00
|
|
|
def getXlationFor( base, loc ):
|
2014-04-24 15:24:38 +02:00
|
|
|
ids = readIDs(base)
|
|
|
|
eng = asMap( base + '/' + english, ids )
|
|
|
|
other = asMap( base + '/' + other_f % (loc), ids )
|
2014-04-21 16:13:41 +02:00
|
|
|
result = []
|
|
|
|
for key in eng.keys():
|
|
|
|
if key in other:
|
2014-04-24 15:24:38 +02:00
|
|
|
result.append( { 'id' : key, 'loc' : other[key] } )
|
2014-04-21 16:13:41 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
def main():
|
2014-04-24 15:24:38 +02:00
|
|
|
data = getXlationFor( '.', 'ba_CK' )
|
|
|
|
print data
|
|
|
|
data = getXlationFor( '.', 'ca_PS' )
|
2014-04-21 16:13:41 +02:00
|
|
|
print data
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|