/Scripts/localize.py

https://gitlab.com/jslee1/WordPress-iOS · Python · 148 lines · 104 code · 33 blank · 11 comment · 27 complexity · e32e2af60a82ad7ac7bd4dd4870bc530 MD5 · raw file

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # This program is free software. It comes without any warranty, to
  4. # the extent permitted by applicable law. You can redistribute it
  5. # and/or modify it under the terms of the Do What The Fuck You Want
  6. # To Public License, Version 2, as published by Sam Hocevar. See
  7. # http://sam.zoy.org/wtfpl/COPYING for more details.
  8. #
  9. # Localize.py - Incremental localization on XCode projects
  10. # João Moreno 2009
  11. # http://joaomoreno.com/
  12. from sys import argv
  13. from codecs import open
  14. from re import compile
  15. from copy import copy
  16. import os
  17. re_translation = compile(r'^"(.+)" = "(.+)";$')
  18. re_comment_single = compile(r'^/(/.*|\*.*\*/)$')
  19. re_comment_start = compile(r'^/\*.*$')
  20. re_comment_end = compile(r'^.*\*/$')
  21. def print_help():
  22. print u"""Usage: merge.py merged_file old_file new_file
  23. Xcode localizable strings merger script. João Moreno 2009."""
  24. class LocalizedString():
  25. def __init__(self, comments, translation):
  26. self.comments, self.translation = comments, translation
  27. self.key, self.value = re_translation.match(self.translation).groups()
  28. def __unicode__(self):
  29. return u'%s%s\n' % (u''.join(self.comments), self.translation)
  30. class LocalizedFile():
  31. def __init__(self, fname=None, auto_read=False):
  32. self.fname = fname
  33. self.strings = []
  34. self.strings_d = {}
  35. if auto_read:
  36. self.read_from_file(fname)
  37. def read_from_file(self, fname=None):
  38. fname = self.fname if fname == None else fname
  39. try:
  40. f = open(fname, encoding='utf_16', mode='r')
  41. except:
  42. print 'File %s does not exist.' % fname
  43. exit(-1)
  44. line = f.readline()
  45. while line and line == u'\n':
  46. line = f.readline()
  47. while line:
  48. comments = [line]
  49. if not re_comment_single.match(line):
  50. while line and not re_comment_end.match(line):
  51. line = f.readline()
  52. comments.append(line)
  53. line = f.readline()
  54. if line and re_translation.match(line):
  55. translation = line
  56. else:
  57. raise Exception('invalid file: %s' % line)
  58. line = f.readline()
  59. while line and line == u'\n':
  60. line = f.readline()
  61. string = LocalizedString(comments, translation)
  62. self.strings.append(string)
  63. self.strings_d[string.key] = string
  64. f.close()
  65. def save_to_file(self, fname=None):
  66. fname = self.fname if fname == None else fname
  67. try:
  68. f = open(fname, encoding='utf_16', mode='w')
  69. except:
  70. print 'Couldn\'t open file %s.' % fname
  71. exit(-1)
  72. for string in self.strings:
  73. f.write(string.__unicode__())
  74. f.close()
  75. def merge_with(self, new):
  76. merged = LocalizedFile()
  77. for string in new.strings:
  78. if self.strings_d.has_key(string.key):
  79. new_string = copy(self.strings_d[string.key])
  80. new_string.comments = string.comments
  81. string = new_string
  82. merged.strings.append(string)
  83. merged.strings_d[string.key] = string
  84. return merged
  85. def merge(merged_fname, old_fname, new_fname):
  86. try:
  87. old = LocalizedFile(old_fname, auto_read=True)
  88. new = LocalizedFile(new_fname, auto_read=True)
  89. except Exception as e:
  90. print 'Error: input files have invalid format. old: %s, new: %s' % (old_fname, new_fname)
  91. print e
  92. merged = old.merge_with(new)
  93. merged.save_to_file(merged_fname)
  94. STRINGS_FILE = 'Localizable.strings'
  95. def localize(path):
  96. if "Scripts" in path:
  97. print "Must run script from the root folder"
  98. quit()
  99. os.chdir(path)
  100. resources_path = os.path.join(path, 'Resources')
  101. language = os.path.join(resources_path, 'en.lproj')
  102. original = merged = language + os.path.sep + STRINGS_FILE
  103. old = original + '.old'
  104. new = original + '.new'
  105. if os.path.isfile(original):
  106. os.rename(original, old)
  107. os.system('genstrings -q -o "%s" `find . ../Pods/WordPress* ../Pods/WPMediaPicker -name "*.m" -o -name "*.swift" | grep -v Vendor`' % language)
  108. os.rename(original, new)
  109. merge(merged, old, new)
  110. os.remove(new)
  111. os.remove(old)
  112. else:
  113. os.system('genstrings -q -o "%s" `find . -name "*.m" -o -name "*.swift"` | grep -v Vendor' % language)
  114. if __name__ == '__main__':
  115. localize(os.path.join(os.getcwd(), 'WordPress'))