daily pastebin goal
60%
SHARE
TWEET

i18n.py

mbpaster May 10th, 2013 144 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import glob
  2. import xml.etree.ElementTree as ET
  3. import os
  4. import sys
  5.  
  6. def parse_strings(xml_file):
  7.         tree = ET.parse(xml_file)
  8.         root = tree.getroot()
  9.         all_strings = {}
  10.  
  11.         for child in root:
  12.                 name = child.get('name')
  13.                 if name in all_strings:
  14.                         print 'WARNING! duplicate key %s in %s' % (name, xml_file)
  15.                 all_strings[name] = child.text
  16.  
  17.         return all_strings
  18.  
  19. def print_missing(missing_set, default_strings):
  20.         for key in missing_set:
  21.                 print '<string name="%s">%s</string>' % (key, default_strings[key])
  22.  
  23. path_to_default = '../res/values/strings.xml'
  24. default_strings = parse_strings(path_to_default)
  25. all_keys = set(default_strings)
  26.  
  27. values_folders = glob.glob('../res/values-[A-Za-z][A-Za-z]')
  28.  
  29. for values_folder in values_folders:
  30.         try:
  31.                 with open(os.path.join(values_folder, 'strings.xml')) as strings_file:
  32.                         translated = parse_strings(strings_file)
  33.                         shared = set(translated).intersection(all_keys)
  34.                         missing = all_keys - shared
  35.                         if missing:
  36.                                 print >> sys.stderr, 'Missing in %s:' % os.path.abspath(strings_file.name)
  37.                                 print_missing(missing, default_strings)
  38.                                 print
  39.  
  40.         except IOError:
  41.                 print "Ignoring %s as it's not an i18n folder" % values_folder
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top