File indexing completed on 2024-05-19 14:56:47

0001 #!/usr/bin/python3
0002 #
0003 # GCompris - datasetToPo.py
0004 #
0005 # SPDX-FileCopyrightText: 2015 Bruno Coudoin <bruno.coudoin@gcompris.net>
0006 #
0007 #   SPDX-License-Identifier: GPL-3.0-or-later
0008 
0009 import sys
0010 import json
0011 import os
0012 import datetime
0013 import polib
0014 import urllib
0015 from urllib.parse import quote
0016 
0017 if(len(sys.argv) < 3):
0018     print("Usage: dataSetToPo.py dataset.json output.po [content-fr.json]")
0019     print("  The optional argument is used to backport manually created json")
0020     sys.exit(1)
0021 
0022 def loadManualFile(manual):
0023     json_data = open(manual)
0024     manualData = json.load(json_data)
0025     json_data.close()
0026     return manualData
0027     
0028 def getManualTranslation(manualData, key):
0029     if not manualData:
0030         return ""
0031     key = key.split("/")[-1]
0032     try:
0033         return manualData[key]
0034     except:
0035         return ""
0036 
0037 manualData = None
0038 if(len(sys.argv) == 4):
0039     manualData = loadManualFile(sys.argv[3])
0040 
0041 dataset = sys.argv[1]
0042 json_data = open(dataset)
0043 data = json.load(json_data)
0044 json_data.close()
0045 
0046 displayInConsole = False
0047 
0048 # Get last modification time of data set
0049 modtime = os.path.getmtime(dataset)
0050 modtime_utc = datetime.datetime.utcfromtimestamp(modtime)
0051 modtime_utc_string = modtime_utc.strftime('%Y-%m-%d %H:%M') + '+0000'
0052 
0053 # Header
0054 po = polib.POFile()
0055 po.metadata = {
0056     'Project-Id-Version': 'gcompris_qt\\n',
0057     'Report-Msgid-Bugs-To': 'https://bugs.kde.org/enter_bug.cgi?product=gcompris',
0058     'POT-Creation-Date': modtime_utc_string,
0059     'PO-Revision-Date': modtime_utc_string,
0060     'Last-Translator': 'FULL NAME <EMAIL@ADDRESS>\n',
0061     'Language-Team': 'LANGUAGE <kde-i18n-doc@kde.org>\n',
0062     'MIME-Version': '1.0',
0063     'Content-Type': 'text/plain; charset=utf-8',
0064     'Content-Transfer-Encoding': '8bit',
0065 }
0066 
0067 for chapter in data:
0068     for lesson in chapter['content']:
0069         for word in lesson['content']:
0070             voice = word['voice'].split('/')[-1].split(".")[0] + ".ogg"
0071             imageLink = "https://gcompris.net/incoming/lang/words_by_section.html#" + \
0072                        urllib.parse.quote(word['description'].split('/')[-1].split(".")[0])
0073             if displayInConsole:
0074                 print("#. " + chapter['name'] + \
0075                       " / " + lesson['name'] + \
0076                       " / " + word['description'] + \
0077                       ": "+ imageLink)
0078                 print('msgctxt "'+voice+'"|"')
0079                 print('msgid "' + word['description'] + '"')
0080                 print('msgstr "' + getManualTranslation(manualData, voice) + '"')
0081                 print("")
0082 
0083             entry = polib.POEntry(
0084                 msgid = word['description'],
0085                 msgstr = getManualTranslation(manualData, voice),
0086                 msgctxt = voice,
0087                 comment = chapter['name'] + " / " + lesson['name'] + " / " + word['description'] +
0088                           "\n" + imageLink
0089             )
0090             po.append(entry)
0091 
0092 po.save(sys.argv[2])