File indexing completed on 2024-04-21 14:43:50

0001 #!/usr/bin/python3
0002 #
0003 # GCompris - mergePo.py
0004 #
0005 # SPDX-FileCopyrightText: 2017 Johnny Jazeix <jazeix@gmail.com>
0006 #
0007 #   SPDX-License-Identifier: GPL-3.0-or-later
0008 
0009 # Using polib.py from https://bitbucket.org/izi/polib/wiki/Home
0010 
0011 # Usage : python3 mergePo.py qt.po gtk.po
0012 # Find in the first argument po file the translated lines of the gtk po file
0013 # (if existing) and replace the translation for the po file.
0014 # For those not found, no translation is provided.
0015 
0016 # something like: python3 mergePo.py gcompris_fi.po fi.po && sed '/^#|/ d' < gcompris_fi.po > qtgl.po && mv qtgl.po gcompris_fi.po
0017 
0018 import sys
0019 import polib
0020 
0021 if len(sys.argv) < 3:
0022     print('Usage : python3 mergePo.py gcompris_qt.po gcompris_gtk.po.'
0023           ' Output will be in the input file!')
0024     sys.exit(1)
0025 
0026 # load an existing po file
0027 po = polib.pofile(sys.argv[1])
0028 poGtk = polib.pofile(sys.argv[2])
0029 
0030 print(po.percent_translated())
0031 
0032 # First remove all fuzzy strings
0033 for entry in po.fuzzy_entries():
0034     entry.msgstr = ''
0035     if entry.msgid_plural:
0036         entry.msgstr_plural['0'] = ''
0037     if entry.msgid_plural and '1' in entry.msgstr_plural:
0038         entry.msgstr_plural['1'] = ''
0039     if entry.msgid_plural and '2' in entry.msgstr_plural:
0040         entry.msgstr_plural['2'] = ''
0041     entry.flags.remove('fuzzy')
0042     po.save()
0043 
0044 # uncomment to create a new po
0045 #for entry in po:
0046 #    if entry.msgstr != "":
0047 #        entry.msgstr = "";
0048 
0049 #print(po.percent_translated())
0050 
0051 # Then replace in the output all the good strings
0052 for entry in po:
0053     if entry.msgstr != "":
0054         continue
0055 
0056     for entryGtk in poGtk:
0057         if entry.msgid.encode('utf-8') == entryGtk.msgid.encode('utf-8') and entryGtk.msgstr != "":
0058             entry.msgstr = entryGtk.msgstr
0059 
0060 po.save()
0061 
0062 print(po.percent_translated())