2014-04-07 17:14:09 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2014-04-09 03:57:31 +02:00
|
|
|
import glob, sys, re, os, getopt
|
|
|
|
|
2014-04-07 17:14:09 +02:00
|
|
|
from lxml import etree
|
|
|
|
|
2014-04-08 15:48:42 +02:00
|
|
|
# sys.exit(0)
|
|
|
|
|
|
|
|
g_xmlTypes = [
|
|
|
|
{ 'elemName': 'item',
|
|
|
|
'attrType' : '{http://schemas.android.com/apk/res/android}title'
|
|
|
|
},
|
|
|
|
{ 'elemName': 'Button',
|
|
|
|
'attrType' : '{http://schemas.android.com/apk/res/android}text'
|
|
|
|
},
|
|
|
|
{ 'elemName': 'TextView',
|
|
|
|
'attrType' : '{http://schemas.android.com/apk/res/android}text'
|
|
|
|
},
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
g_pairs = {}
|
2014-04-07 17:14:09 +02:00
|
|
|
|
|
|
|
STR_REF = re.compile('@string/(.*)$')
|
|
|
|
|
|
|
|
def xform(src, dest):
|
|
|
|
doc = etree.parse(src)
|
2014-04-08 15:48:42 +02:00
|
|
|
root = doc.getroot();
|
|
|
|
for child in root.iter():
|
|
|
|
for elem in g_xmlTypes:
|
|
|
|
if child.tag == elem['elemName']:
|
|
|
|
value = child.get(elem['attrType'])
|
|
|
|
match = value and STR_REF.match(value)
|
|
|
|
if match:
|
|
|
|
key = match.group(1)
|
|
|
|
if key in g_pairs:
|
|
|
|
child.set(elem['attrType'], "loc:" + key)
|
|
|
|
|
|
|
|
# create directory if needed, then write file
|
|
|
|
dir = os.path.dirname( dest )
|
|
|
|
if not os.path.exists(dir): os.makedirs(dir)
|
2014-04-07 17:14:09 +02:00
|
|
|
doc.write( dest, pretty_print=True )
|
|
|
|
|
2014-04-09 03:57:31 +02:00
|
|
|
def printStrings( pairs, outfile ):
|
|
|
|
fil = open( outfile, "w" )
|
|
|
|
|
|
|
|
# beginning of the class file
|
|
|
|
lines = """
|
|
|
|
/***********************************************************************
|
|
|
|
* Generated file; do not edit!!!
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
package org.eehouse.android.xw4.loc;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import org.eehouse.android.xw4.R;
|
|
|
|
|
|
|
|
public class LocIDsData {
|
|
|
|
public static final int NOT_FOUND = -1;
|
|
|
|
|
|
|
|
protected static final Map<String, Integer> S_MAP =
|
|
|
|
Collections.unmodifiableMap(new HashMap<String, Integer>() {{
|
|
|
|
"""
|
|
|
|
fil.write( lines )
|
|
|
|
|
|
|
|
for key in pairs.keys():
|
|
|
|
fil.write( " put(\"%s\", R.string.%s);\n" % (key, key) )
|
|
|
|
|
|
|
|
# Now the end of the class
|
|
|
|
lines = """
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
/* end generated file */
|
|
|
|
"""
|
|
|
|
fil.write( lines )
|
|
|
|
|
|
|
|
def main():
|
|
|
|
outfile = ''
|
|
|
|
pairs, rest = getopt.getopt(sys.argv[1:], "o:")
|
|
|
|
for option, value in pairs:
|
|
|
|
if option == '-o': outfile = value
|
|
|
|
|
|
|
|
# Gather all localizable strings
|
|
|
|
for path in glob.iglob( "res/values/strings.xml" ):
|
|
|
|
for action, elem in etree.iterparse(path):
|
|
|
|
if "end" == action and 'string' == elem.tag:
|
|
|
|
g_pairs[elem.get('name')] = True
|
|
|
|
|
|
|
|
for subdir, dirs, files in os.walk('res_src'):
|
|
|
|
for file in files:
|
|
|
|
src = subdir + '/' + file
|
|
|
|
dest = src.replace( 'res_src', 'res', 1 )
|
|
|
|
xform( src, dest )
|
|
|
|
|
|
|
|
printStrings( g_pairs, outfile )
|
|
|
|
|
|
|
|
|
|
|
|
main()
|