Created
September 18, 2012 10:31
-
-
Save nolanlawson/3742499 to your computer and use it in GitHub Desktop.
Python script to help with outputting Android strings.xml files in an organized format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh/env python | |
# organize the strings.xml files used for Android projects | |
# assumes I have some strings in the English one that don't need to be translated, so these | |
# are separated from the rest. Also assumes I always keep up with the French translations. | |
# | |
# usage: just run it from the res/ folder | |
# | |
import sys, re, os | |
from xml.dom.minidom import parseString | |
reload(sys) | |
sys.setdefaultencoding('utf-8') | |
langs = ['en','fr','ja','de'] | |
lookup = {} | |
for lang in langs: | |
lookup[lang] = {} | |
for dirname in os.listdir('.'): | |
for lang in langs: | |
values = 'values-%s' % lang if lang != 'en' else 'values' | |
if (dirname == values): | |
stringsfile = open(dirname + '/strings.xml','r').read() | |
dom = parseString(stringsfile) | |
strings = dom.getElementsByTagName('string') | |
for string in strings: | |
lookup[lang][string.getAttribute('name')] = string.toxml() | |
# Assume that strings that exist in French but not in Japanese or German need to be translated. | |
# I'm pretty good about keeping up with the French translations | |
for (key,value) in list(lookup['fr'].items()): | |
for lang in ['ja','de']: | |
if key not in lookup[lang] or lookup[lang][key] == lookup['en'][key]: | |
lookup[lang][key] = '<!-- TO TRANSLATE -->\n ' + lookup['en'][key] | |
for lang in langs: | |
values = 'values-%s' % lang if lang != 'en' else 'values' | |
stringsfile = open('%s/strings.xml' % values,'w') | |
stringsfile.write('<?xml version="1.0" encoding="utf-8"?>\n<resources>\n') | |
sortedItems = sorted(lookup[lang].items()) | |
# for English, do special logic to separate translateable from non-translateable strings | |
if (lang == 'en'): | |
firstSet = {} | |
secondSet = {} | |
for (key,value) in lookup[lang].items(): | |
if key not in lookup['fr']: # assume I translated everything that needed to be translated for French | |
firstSet[key] = value | |
else: | |
secondSet[key] =value | |
header1 = [('',''),('','<!-- non-translateable strings-->'),('','')] | |
header2 = [('',''),('','<!-- translateable strings-->'),('','')] | |
sortedItems = header1 + sorted(firstSet.items()) + header2 + sorted(secondSet.items()) | |
# assume I have a nice naming convention where the string names are prefixed with e.g. 'text_','title_' | |
last_prefix = None | |
for (name, xmlLine) in sortedItems: | |
prefix = name.split('_')[0] | |
if last_prefix != None and prefix != last_prefix: | |
stringsfile.write(' \n') | |
last_prefix = prefix | |
stringsfile.write(' ' + xmlLine + '\n') | |
stringsfile.write('</resources>') | |
stringsfile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment