/tools/dev/po-merge.py

https://github.com/yuvrajm/subversion · Python · 191 lines · 136 code · 16 blank · 39 comment · 54 complexity · 6d100d828f298d5800e78bbe7219aa1c MD5 · raw file

  1. #!/usr/bin/env python
  2. #
  3. #
  4. # Licensed to the Apache Software Foundation (ASF) under one
  5. # or more contributor license agreements. See the NOTICE file
  6. # distributed with this work for additional information
  7. # regarding copyright ownership. The ASF licenses this file
  8. # to you under the Apache License, Version 2.0 (the
  9. # "License"); you may not use this file except in compliance
  10. # with the License. You may obtain a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing,
  15. # software distributed under the License is distributed on an
  16. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. # KIND, either express or implied. See the License for the
  18. # specific language governing permissions and limitations
  19. # under the License.
  20. #
  21. #
  22. import os, re, sys
  23. msgstr_re = re.compile('msgstr\[\d+\] "')
  24. def parse_translation(f):
  25. """Read a single translation entry from the file F and return a
  26. tuple with the comments, msgid, msgid_plural and msgstr. The comments is
  27. returned as a list of lines which do not end in new-lines. The msgid is
  28. string. The msgid_plural is string or None. The msgstr is a list of
  29. strings. The msgid, msgid_plural and msgstr strings can contain embedded
  30. newlines"""
  31. line = f.readline()
  32. # Parse comments
  33. comments = []
  34. while True:
  35. if line.strip() == '' or line[:2] == '#~':
  36. return comments, None, None, None
  37. elif line[0] == '#':
  38. comments.append(line[:-1])
  39. else:
  40. break
  41. line = f.readline()
  42. # Parse msgid
  43. if line[:7] != 'msgid "' or line[-2] != '"':
  44. raise RuntimeError("parse error")
  45. msgid = line[6:-1]
  46. while True:
  47. line = f.readline()
  48. if line[0] != '"':
  49. break
  50. msgid += '\n' + line[:-1]
  51. # Parse optional msgid_plural
  52. msgid_plural = None
  53. if line[:14] == 'msgid_plural "':
  54. if line[-2] != '"':
  55. raise RuntimeError("parse error")
  56. msgid_plural = line[13:-1]
  57. while True:
  58. line = f.readline()
  59. if line[0] != '"':
  60. break
  61. msgid_plural += '\n' + line[:-1]
  62. # Parse msgstr
  63. msgstr = []
  64. if not msgid_plural:
  65. if line[:8] != 'msgstr "' or line[-2] != '"':
  66. raise RuntimeError("parse error")
  67. msgstr.append(line[7:-1])
  68. while True:
  69. line = f.readline()
  70. if len(line) == 0 or line[0] != '"':
  71. break
  72. msgstr[0] += '\n' + line[:-1]
  73. else:
  74. if line[:7] != 'msgstr[' or line[-2] != '"':
  75. raise RuntimeError("parse error")
  76. i = 0
  77. while True:
  78. matched_msgstr = msgstr_re.match(line)
  79. if matched_msgstr:
  80. matched_msgstr_len = len(matched_msgstr.group(0))
  81. msgstr.append(line[matched_msgstr_len-1:-1])
  82. else:
  83. break
  84. while True:
  85. line = f.readline()
  86. if len(line) == 0 or line[0] != '"':
  87. break
  88. msgstr[i] += '\n' + line[:-1]
  89. i += 1
  90. if line.strip() != '':
  91. raise RuntimeError("parse error")
  92. return comments, msgid, msgid_plural, msgstr
  93. def split_comments(comments):
  94. """Split COMMENTS into flag comments and other comments. Flag
  95. comments are those that begin with '#,', e.g. '#,fuzzy'."""
  96. flags = []
  97. other = []
  98. for c in comments:
  99. if len(c) > 1 and c[1] == ',':
  100. flags.append(c)
  101. else:
  102. other.append(c)
  103. return flags, other
  104. def main(argv):
  105. if len(argv) != 2:
  106. argv0 = os.path.basename(argv[0])
  107. sys.exit('Usage: %s <lang.po>\n'
  108. '\n'
  109. 'This script will replace the translations and flags in lang.po with\n'
  110. 'the translations and flags in the source po file read from standard\n'
  111. 'input. Strings that are not found in the source file are left untouched.\n'
  112. 'A backup copy of lang.po is saved as lang.po.bak.\n'
  113. '\n'
  114. 'Example:\n'
  115. ' svn cat http://svn.apache.org/repos/asf/subversion/trunk/subversion/po/sv.po | \\\n'
  116. ' %s sv.po' % (argv0, argv0))
  117. # Read the source po file into a hash
  118. source = {}
  119. while True:
  120. comments, msgid, msgid_plural, msgstr = parse_translation(sys.stdin)
  121. if not comments and msgid is None:
  122. break
  123. if msgid is not None:
  124. source[msgid] = msgstr, split_comments(comments)[0]
  125. # Make a backup of the output file, open the copy for reading
  126. # and the original for writing.
  127. os.rename(argv[1], argv[1] + '.bak')
  128. infile = open(argv[1] + '.bak')
  129. outfile = open(argv[1], 'w')
  130. # Loop thought the original and replace stuff as we go
  131. first = 1
  132. string_count = 0
  133. update_count = 0
  134. untranslated = 0
  135. while True:
  136. comments, msgid, msgid_plural, msgstr = parse_translation(infile)
  137. if not comments and msgid is None:
  138. break
  139. if not first:
  140. outfile.write('\n')
  141. first = 0
  142. if msgid is None:
  143. outfile.write('\n'.join(comments) + '\n')
  144. else:
  145. string_count += 1
  146. # Do not update the header, and only update if the source
  147. # has a non-empty translation.
  148. if msgid != '""' and source.get(msgid, ['""', []])[0] != '""':
  149. other = split_comments(comments)[1]
  150. new_msgstr, new_flags = source[msgid]
  151. new_comments = other + new_flags
  152. if new_msgstr != msgstr or new_comments != comments:
  153. update_count += 1
  154. msgstr = new_msgstr
  155. comments = new_comments
  156. outfile.write('\n'.join(comments) + '\n')
  157. outfile.write('msgid ' + msgid + '\n')
  158. if not msgid_plural:
  159. outfile.write('msgstr ' + msgstr[0] + '\n')
  160. else:
  161. outfile.write('msgid_plural ' + msgid_plural + '\n')
  162. n = 0
  163. for i in msgstr:
  164. outfile.write('msgstr[%s] %s\n' % (n, msgstr[n]))
  165. n += 1
  166. for m in msgstr:
  167. if m == '""':
  168. untranslated += 1
  169. # We're done. Tell the user what we did.
  170. print(('%d strings updated. '
  171. '%d of %d strings are still untranslated (%.0f%%).' %
  172. (update_count, untranslated, string_count,
  173. 100.0 * untranslated / string_count)))
  174. if __name__ == '__main__':
  175. main(sys.argv)