1#!/usr/bin/python3
2#
3# GCompris - datasetToPo.py
4#
5# SPDX-FileCopyrightText: 2015 Bruno Coudoin <bruno.coudoin@gcompris.net>
6#
7#   SPDX-License-Identifier: GPL-3.0-or-later
8
9import sys
10import json
11import os
12import datetime
13import polib
14import urllib
15from urllib.parse import quote
16
17if(len(sys.argv) < 3):
18    print("Usage: dataSetToPo.py dataset.json output.po [content-fr.json]")
19    print("  The optional argument is used to backport manually created json")
20    sys.exit(1)
21
22def loadManualFile(manual):
23    json_data = open(manual)
24    manualData = json.load(json_data)
25    json_data.close()
26    return manualData
27
28def getManualTranslation(manualData, key):
29    if not manualData:
30        return ""
31    key = key.split("/")[-1]
32    try:
33        return manualData[key]
34    except:
35        return ""
36
37manualData = None
38if(len(sys.argv) == 4):
39    manualData = loadManualFile(sys.argv[3])
40
41dataset = sys.argv[1]
42json_data = open(dataset)
43data = json.load(json_data)
44json_data.close()
45
46displayInConsole = False
47
48# Get last modification time of data set
49modtime = os.path.getmtime(dataset)
50modtime_utc = datetime.datetime.utcfromtimestamp(modtime)
51modtime_utc_string = modtime_utc.strftime('%Y-%m-%d %H:%M') + '+0000'
52
53# Header
54po = polib.POFile()
55po.metadata = {
56    'Project-Id-Version': 'gcompris_qt\\n',
57    'Report-Msgid-Bugs-To': 'https://bugs.kde.org/enter_bug.cgi?product=gcompris',
58    'POT-Creation-Date': modtime_utc_string,
59    'PO-Revision-Date': modtime_utc_string,
60    'Last-Translator': 'FULL NAME <EMAIL@ADDRESS>\n',
61    'Language-Team': 'LANGUAGE <kde-i18n-doc@kde.org>\n',
62    'MIME-Version': '1.0',
63    'Content-Type': 'text/plain; charset=utf-8',
64    'Content-Transfer-Encoding': '8bit',
65}
66
67for chapter in data:
68    for lesson in chapter['content']:
69        for word in lesson['content']:
70            voice = word['voice'].split('/')[-1].split(".")[0] + ".ogg"
71            imageLink = "https://gcompris.net/incoming/lang/words_by_section.html#" + \
72                       urllib.parse.quote(word['description'].split('/')[-1].split(".")[0])
73            if displayInConsole:
74                print("#. " + chapter['name'] + \
75                      " / " + lesson['name'] + \
76                      " / " + word['description'] + \
77                      ": "+ imageLink)
78                print('msgctxt "'+voice+'"|"')
79                print('msgid "' + word['description'] + '"')
80                print('msgstr "' + getManualTranslation(manualData, voice) + '"')
81                print("")
82
83            entry = polib.POEntry(
84                msgid = word['description'],
85                msgstr = getManualTranslation(manualData, voice),
86                msgctxt = voice,
87                comment = chapter['name'] + " / " + lesson['name'] + " / " + word['description'] +
88                          "\n" + imageLink
89            )
90            po.append(entry)
91
92po.save(sys.argv[2])
93