merge_po.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env python3
  2. # ***** BEGIN GPL LICENSE BLOCK *****
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software Foundation,
  16. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. #
  18. # ***** END GPL LICENSE BLOCK *****
  19. # <pep8 compliant>
  20. # Merge one or more .po files into the first dest one.
  21. # If a msgkey is present in more than one merged po, the one in the first file wins, unless
  22. # it’s marked as fuzzy and one later is not.
  23. # The fuzzy flag is removed if necessary.
  24. # All other comments are never modified.
  25. # However, commented messages in dst will always remain commented, and commented messages are
  26. # never merged from sources.
  27. import sys
  28. if __package__ is None:
  29. import settings
  30. import utils
  31. else:
  32. from . import (
  33. settings,
  34. utils,
  35. )
  36. # XXX This is a quick hack to make it work with new I18n... objects! To be reworked!
  37. def main():
  38. import argparse
  39. parser = argparse.ArgumentParser(
  40. description=(
  41. "Merge one or more .po files into the first dest one.\n"
  42. "If a msgkey (msgctxt, msgid) is present in more than one merged po, the one in the first file "
  43. "wins, unless it’s marked as fuzzy and one later is not.\n"
  44. "The fuzzy flag is removed if necessary.\n"
  45. "All other comments are never modified.\n"
  46. "Commented messages in dst will always remain commented, and commented messages are never merged "
  47. "from sources."
  48. ),
  49. )
  50. parser.add_argument('-s', '--stats', action="store_true", help="Show statistics info.")
  51. parser.add_argument('-r', '--replace', action="store_true",
  52. help="Replace existing messages of same \"level\" already in dest po.")
  53. parser.add_argument('dst', metavar='dst.po', help="The dest po into which merge the others.")
  54. parser.add_argument('src', metavar='src.po', nargs='+', help="The po's to merge into the dst.po one.")
  55. args = parser.parse_args()
  56. ret = 0
  57. done_msgkeys = set()
  58. done_fuzzy_msgkeys = set()
  59. nbr_merged = 0
  60. nbr_replaced = 0
  61. nbr_added = 0
  62. nbr_unfuzzied = 0
  63. dst_msgs = utils.I18nMessages(kind='PO', src=args.dst)
  64. if dst_msgs.parsing_errors:
  65. print("Dest po is BROKEN, aborting.")
  66. return 1
  67. if args.stats:
  68. print("Dest po, before merging:")
  69. dst_msgs.print_stats(prefix="\t")
  70. # If we don’t want to replace existing valid translations, pre-populate done_msgkeys and done_fuzzy_msgkeys.
  71. if not args.replace:
  72. done_msgkeys = dst_msgs.trans_msgs.copy()
  73. done_fuzzy_msgkeys = dst_msgs.fuzzy_msgs.copy()
  74. for po in args.src:
  75. msgs = utils.I18nMessages(kind='PO', src=po)
  76. if msgs.parsing_errors:
  77. print("\tSrc po {} is BROKEN, skipping.".format(po))
  78. ret = 1
  79. continue
  80. print("\tMerging {}...".format(po))
  81. if args.stats:
  82. print("\t\tMerged po stats:")
  83. msgs.print_stats(prefix="\t\t\t")
  84. for msgkey, msg in msgs.msgs.items():
  85. msgctxt, msgid = msgkey
  86. # This msgkey has already been completely merged, or is a commented one,
  87. # or the new message is commented, skip it.
  88. if msgkey in (done_msgkeys | dst_msgs.comm_msgs | msgs.comm_msgs):
  89. continue
  90. is_ttip = msg.is_tooltip
  91. # New messages does not yet exists in dest.
  92. if msgkey not in dst_msgs.msgs:
  93. dst_msgs[msgkey] = msgs.msgs[msgkey]
  94. if msgkey in msgs.fuzzy_msgs:
  95. done_fuzzy_msgkeys.add(msgkey)
  96. dst_msgs.fuzzy_msgs.add(msgkey)
  97. elif msgkey in msgs.trans_msgs:
  98. done_msgkeys.add(msgkey)
  99. dst_msgs.trans_msgs.add(msgkey)
  100. nbr_added += 1
  101. # From now on, the new messages is already in dst.
  102. # New message is neither translated nor fuzzy, skip it.
  103. elif msgkey not in (msgs.trans_msgs | msgs.fuzzy_msgs):
  104. continue
  105. # From now on, the new message is either translated or fuzzy!
  106. # The new message is translated.
  107. elif msgkey in msgs.trans_msgs:
  108. dst_msgs.msgs[msgkey].msgstr = msg.msgstr
  109. done_msgkeys.add(msgkey)
  110. done_fuzzy_msgkeys.discard(msgkey)
  111. if msgkey in dst_msgs.fuzzy_msgs:
  112. dst_msgs.fuzzy_msgs.remove(msgkey)
  113. nbr_unfuzzied += 1
  114. if msgkey not in dst_msgs.trans_msgs:
  115. dst_msgs.trans_msgs.add(msgkey)
  116. else:
  117. nbr_replaced += 1
  118. nbr_merged += 1
  119. # The new message is fuzzy, org one is fuzzy too, and this msgkey has not yet been merged.
  120. elif msgkey not in (dst_msgs.trans_msgs | done_fuzzy_msgkeys):
  121. dst_msgs[msgkey].msgstr = msg.msgstr
  122. done_fuzzy_msgkeys.add(msgkey)
  123. dst_msgs.fuzzy_msgs.add(msgkey)
  124. nbr_merged += 1
  125. nbr_replaced += 1
  126. dst_msgs.write(kind='PO', dest=args.dst)
  127. print("Merged completed. {} messages were merged (among which {} were replaced), {} were added, "
  128. "{} were \"un-fuzzied\".".format(nbr_merged, nbr_replaced, nbr_added, nbr_unfuzzied))
  129. if args.stats:
  130. dst_msgs.update_info()
  131. print("Final merged po stats:")
  132. dst_msgs.print_stats(prefix="\t")
  133. return ret
  134. if __name__ == "__main__":
  135. print("\n\n *** Running {} *** \n".format(__file__))
  136. sys.exit(main())