sync_languages.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # TODO: Comment this code
  4. import sys
  5. import shutil
  6. import os
  7. from gluon.languages import findT
  8. sys.path.insert(0, '.')
  9. def sync_language(d, data):
  10. ''' this function makes sure a translated string will be prefered over an untranslated
  11. string when syncing languages between apps. when both are translated, it prefers the
  12. latter app, as did the original script
  13. '''
  14. for key in data:
  15. # if this string is not in the allready translated data, add it
  16. if key not in d:
  17. d[key] = data[key]
  18. # see if there is a translated string in the original list, but not in the new list
  19. elif (
  20. ((d[key] != '') or (d[key] != key)) and
  21. ((data[key] == '') or (data[key] == key))
  22. ):
  23. d[key] = d[key]
  24. # any other case (wether there is or there isn't a translated string)
  25. else:
  26. d[key] = data[key]
  27. return d
  28. def sync_main(file, apps):
  29. d = {}
  30. for app in apps:
  31. path = 'applications/%s/' % app
  32. findT(path, file)
  33. langfile = open(os.path.join(path, 'languages', '%s.py' % file))
  34. try:
  35. data = eval(langfile.read())
  36. finally:
  37. langfile.close()
  38. d = sync_language(d, data)
  39. path = 'applications/%s/' % apps[-1]
  40. file1 = os.path.join(path, 'languages', '%s.py' % file)
  41. f = open(file1, 'w')
  42. try:
  43. f.write('# coding: utf8\n')
  44. f.write('{\n')
  45. keys = d.keys()
  46. keys.sort()
  47. for key in keys:
  48. f.write("'''%s''':'''%s''',\n" % (key.replace("'", "\\'"), str(d[key].replace("'", "\\'"))))
  49. f.write('}\n')
  50. finally:
  51. f.close()
  52. oapps = reversed(apps[:-1])
  53. for app in oapps:
  54. path2 = 'applications/%s/' % app
  55. file2 = os.path.join(path2, 'languages', '%s.py' % file)
  56. if file1 != file2:
  57. shutil.copyfile(file1, file2)
  58. if __name__ == "__main__":
  59. file = sys.argv[1]
  60. apps = sys.argv[2:]
  61. sync_main(file, apps)