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

0001 #!/usr/bin/python3
0002 #
0003 # GCompris - android_format_changelog.py
0004 #
0005 # SPDX-FileCopyrightText: 2023 Johnny Jazeix <jazeix@gmail.com>
0006 #
0007 #   SPDX-License-Identifier: GPL-3.0-or-later
0008 
0009 import sys
0010 import os
0011 import re
0012 import glob
0013 
0014 import polib
0015 from PyQt5.QtCore import QCoreApplication, QUrl
0016 from PyQt5.QtQml import qmlRegisterType, QQmlComponent, QQmlEngine
0017 from python.ActivityInfo import ActivityInfo
0018 
0019 if len(sys.argv) < 2:
0020     print("Usage: android_format_changelog.py <version> [-v]")
0021     sys.exit(1)
0022 
0023 version = sys.argv[1]
0024 verbose = len(sys.argv) >= 3 and sys.argv[2] == "-v"
0025 
0026 def generate_for_locale(changelog_qml, locale):
0027     gcompris_locale = locale.replace("-", "_")
0028 
0029     if gcompris_locale == "iw_IL": # android still uses the old iw iso code for Hebrew in their file
0030         gcompris_locale = "he_IL"
0031     # We use polib to translate as QTranslator only handles .qm files and we don't need to have GCompris compiled to generate this file
0032     translation_file_path = 'poqm/'+gcompris_locale+'/gcompris_qt.po'
0033     if not os.path.isfile(translation_file_path):
0034         # Short locale
0035         gcompris_locale = gcompris_locale[0:gcompris_locale.find("_")]
0036         translation_file_path = 'poqm/'+gcompris_locale+'/gcompris_qt.po'
0037         if not os.path.isfile(translation_file_path):
0038             if verbose:
0039                 print("Locale %s [%s] not handled, skip it" % (locale, gcompris_locale))
0040             return False, ""
0041     if verbose:
0042         print("Generate for locale %s [%s]" % (locale, gcompris_locale))
0043     po = polib.pofile(translation_file_path)
0044 
0045     output = "<%s>\n" % locale
0046 
0047     # Get new activities for this version
0048     # With regex for each ActivityInfo.qml, no need to use Qt there
0049     for activityInfo in glob.glob('src/activities/*/ActivityInfo.qml'):
0050         try:
0051             if activityInfo.find("template") != -1:
0052                 continue
0053             with open(activityInfo) as f:
0054                 content = f.readlines()
0055                 title = ""
0056                 for line in content:
0057                     titleMatch = re.match('.*title:.*\"(.*)\"', line)
0058                     if titleMatch:
0059                         title = titleMatch.group(1)
0060 
0061                     m = re.match('.*createdInVersion:(.*)', line)
0062                     if m:
0063                         activityVersion = m.group(1)
0064                         if int(activityVersion) == int(version):
0065                             po_title = po.find(title)
0066                             if locale == "en-US":
0067                                 translated_title = title
0068                             elif po_title and po_title.translated():
0069                                 translated_title = po_title.msgstr
0070                             else: # The translation is not complete, we skip the language
0071                                 if verbose:
0072                                     print("Skip %s because %s is not translated" % (locale, title))
0073                                 return False, ""
0074                             output += "- " + translated_title + '\n'
0075         except IOError as e:
0076             if verbose:
0077                 print(f"ERROR: Failed to parse {activityInfo}: {e.strerror}")
0078 
0079     # Get changelog information
0080     versions_data = changelog_qml.property('changelog')
0081     for version_data in versions_data.toVariant():
0082         if version == str(version_data['versionCode']):
0083             content = version_data['content']
0084             for feature in content:
0085                 po_feature = po.find(feature)
0086                 if locale == "en-US":
0087                     translated_feature = feature
0088                 elif po_feature and po_feature.translated():
0089                     translated_feature = po.find(feature).msgstr
0090                 else: # The translation is not complete, we skip the language
0091                     if verbose:
0092                         print("Skip %s because %s is not translated" % (locale, feature))
0093                     return False, ""
0094 
0095                 output += "- " + translated_feature + '\n'
0096 
0097     output += "</%s>\n" % locale
0098     return True, output
0099 
0100 def main(argv):
0101     if sys.argv[0] != "./tools/android_format_changelog.py":
0102         print("Needs to be run from top level of GCompris")
0103         sys.exit(1)
0104     app = QCoreApplication(sys.argv)
0105 
0106     engine = QQmlEngine()
0107     # We need to register at least one file for GCompris package else it is not found
0108     qmlRegisterType(ActivityInfo, "GCompris", 1, 0, "ActivityInfo")
0109 
0110     # TODO Need to check if we hardcode the list from the xml file or if we get it in GCompris...
0111 
0112     component = QQmlComponent(engine)
0113     component.loadUrl(QUrl("src/core/ChangeLog.qml"))
0114     changelog_qml = component.create()
0115 
0116     output = ""
0117     # List taken from the android list in https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/locale_config.xml
0118     for locale in ["en-US", "az-AZ", "be", "ca", "cs-CZ", "de-DE", "el-GR", "en-GB", "es-ES", "eu-ES", "fi-FI", "fr-FR", "gl-ES", "hr", "hu-HU", "id", "it-IT", "iw-IL", "lt", "mk-MK", "ml-IN", "nl-NL", "no-NO", "pl-PL", "pt-BR", "pt-PT", "ro", "ru-RU", "sk", "sl", "sq", "sv-SE", "tr-TR", "uk", "zh-CN", "zh-TW"]:
0119         is_translation_ok, locale_changes = generate_for_locale(changelog_qml, locale)
0120         if is_translation_ok:
0121             output += locale_changes
0122 
0123     print(output)
0124 
0125 if __name__ == '__main__':
0126     main(sys.argv)