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

0001 # -* coding: utf-8 -*-
0002 #!/usr/bin/python3
0003 #
0004 # GCompris - po2Dataset.py
0005 #
0006 # SPDX-FileCopyrightText: 2019 Johnny Jazeix <jazeix@gmail.com>
0007 #
0008 #   SPDX-License-Identifier: GPL-3.0-or-later
0009 
0010 import json
0011 import polib
0012 import sys
0013 
0014 if len(sys.argv) != 3:
0015     print("Usage: po2Dataset.py po_file json_file")
0016     sys.exit(1)
0017 
0018 poFile = polib.pofile(sys.argv[1], encoding='utf-8')
0019 jsonFile = sys.argv[2]
0020 data = {}
0021 
0022 if poFile.percent_translated() < 40:
0023     print("Need at least 40% of the words translated to create the json data file for", jsonFile)
0024     sys.exit(0)
0025     
0026 for entry in poFile.translated_entries():
0027     word = entry.msgctxt
0028     data[word] = entry.msgstr
0029 for entry in poFile.untranslated_entries():
0030     word = entry.msgctxt
0031     data[word] = entry.msgstr
0032 for entry in poFile.fuzzy_entries():
0033     word = entry.msgctxt
0034     data[word] = ""
0035 
0036 with open(jsonFile, "w", encoding='utf-8') as data_file:
0037     json.dump(data, data_file, indent=4, sort_keys=True, ensure_ascii=False)
0038     data_file.write("\n") # Add missing new line at end of file