create_languages_js.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python
  2. import sys, os, re
  3. try:
  4. import chardet
  5. except ImportError:
  6. print "You need universal encoding detector for this script"
  7. print " http://chardet.feedparser.org or apt-get install python-chardet"
  8. sys.exit()
  9. regexp_language = re.compile("\* +(.+) +translation", re.IGNORECASE)
  10. js_template = """/* This file is automaticly generated by create_language_js.py */
  11. // some data used in the examples
  12. Ext.namespace('Ext.exampledata');
  13. // TODO: complete and sort the list
  14. Ext.exampledata.languages = [
  15. %s
  16. ];
  17. """
  18. lang_dubs = {}
  19. def lang_name(file):
  20. language = os.path.basename(file)
  21. m = regexp_language.search(open(file).read(512))
  22. if m:
  23. language = m.groups()[0]
  24. if not lang_dubs.has_key(language):
  25. lang_dubs[language] = file
  26. else:
  27. raise Exception('Duplicate language '+language+' for file '+file)
  28. return language
  29. def print_locale(lang_code):
  30. print lang_code,
  31. sys.stdout.flush()
  32. return True
  33. def main():
  34. base_dir = "../../src/locale"
  35. base_file = lambda f: os.path.join(base_dir, f)
  36. try:
  37. locales = os.listdir(base_dir)
  38. except IOError:
  39. print "Cannot find source locale directory: %s ... exiting" % base_dir
  40. sys.exit()
  41. valid_file = lambda e: e.endswith(".js") and e.startswith("ext-lang-")
  42. char_set = lambda f: chardet.detect(open(f).read())['encoding']
  43. lang_code = lambda f: f[9:f.rfind(".js")]
  44. info_set = lambda f: (lang_name(base_file(f)), (lang_code(f), char_set(base_file(f))))
  45. locales = dict(info_set(file) for file in locales if valid_file(file) and print_locale(lang_code(file)))
  46. print "... done"
  47. locale_strarray = ',\n'.join(["\t[%r, %r, %r]" % (code, name, charset) \
  48. for name, (code, charset) in sorted(locales.items())])
  49. # create languages.js
  50. open("languages.js", "w").write(js_template % locale_strarray)
  51. print "saved %d languages to languages.js" % len(locales)
  52. if __name__=="__main__":
  53. main()