From 1d3f14d1b60448aa3fb368e1b457d31598ab5d33 Mon Sep 17 00:00:00 2001 From: Yuri Chornoivan Date: Sat, 9 Jan 2021 09:33:44 +0200 Subject: Add Mageia translation for Plasma JSON --- plasma_json/po2json.py | 120 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100755 plasma_json/po2json.py (limited to 'plasma_json/po2json.py') diff --git a/plasma_json/po2json.py b/plasma_json/po2json.py new file mode 100755 index 00000000..1740a044 --- /dev/null +++ b/plasma_json/po2json.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import errno, glob, polib, re, os, getopt, sys +from time import strftime + +def usage(): + print('\nUsage: python %s [OPTION]' %os.path.basename(sys.argv[0])) + print(' generate pot catalogs and updates po files for json resources in the specified directory') + print('Options: -h, --help : usage') + print(' -d , --directory : directory with json files') + sys.exit(2) +try: + opts, args = getopt.getopt(sys.argv[1:], "hd:", ["help", "directory="]) +except getopt.GetoptError: + usage() # print help information and exit + +directory='.' +for o,a in opts: + if o in ("-h", "--help"): + usage() + if o in ("-d", "--directory"): + directory=a + +directory = directory.rstrip('/') + +if (directory != '') and (os.path.isdir(directory) == False): + sys.exit('Specified directory does not exist') + +# Find all json files +files = [] +for rootdir, dirnames, filenames in os.walk(directory): + files.extend(glob.glob(rootdir + "/*.json")) + +# Define Templates and po directory name +messagetemplate='(?<=\n)(\ *\"Name\".*?\n|\ *\"Description\".*?\n|\ *\"Keywords\".*?\n)' +mpattern=re.compile(messagetemplate,re.DOTALL) +translationtemplate='(?<=\n)(\ *\"Name\[.*?\n|\ *\"Description\[.*?\n|\ *\"Keywords\[.*?\n)' +tpattern=re.compile(translationtemplate,re.DOTALL) +podir = 'po' + +# Write POT file +pot = polib.POFile('',check_for_duplicates=True) +potcreationtime = strftime('%Y-%m-%d %H:%M%z') +pot.metadata = { + 'Project-Id-Version': 'Mageia json files translation', + 'Report-Msgid-Bugs-To': 'i18n-discuss@ml.mageia.org', + 'POT-Creation-Date': potcreationtime, + 'PO-Revision-Date': 'YEAR-MO-DA HO:MI+ZONE', + 'Last-Translator': 'FULL NAME ', + 'Language-Team': 'LANGUAGE ', + 'MIME-Version': '1.0', + 'Content-Type': 'text/plain; charset=UTF-8', + 'Content-Transfer-Encoding': '8bit', + } + +for langfile in files: + langfiledir = langfile.replace('.json', '') + langfilename = langfiledir.rpartition('/')[2] + # Create localization directories if needed + try: + os.makedirs(podir) + except OSError as e: + if e.errno != errno.EEXIST: + raise + #open json file + text = open(langfile,"r").read() + + # Parse contents and add them to POT + for mblock in mpattern.findall(text): + mblock_stripped = mblock.strip('\n') + message_comment, message_id = mblock.strip('\n').split(': ') + message_comment = message_comment.strip('\"') + message_id = message_id.strip('\"') + potentry = polib.POEntry( + msgctxt = message_comment.strip(' \"'), + msgid = message_id.strip(' \",\n'), + msgstr = '', + occurrences=[(langfile,'')] + ) + if message_id != '': + try: + pot.append(potentry) + except ValueError: + print('The entry already exists') +pot.save('./po/json.pot') + +# Merge translations +for pofile in glob.glob(podir + '/*.po'): + lang = pofile[:-3].rsplit('/',1)[1] + pofilename = pofile + po = polib.pofile(pofilename) + po.merge(pot) + po.save(pofilename) + +for langfile in files: + #open json file + jsonfile = open(langfile,"r") + text = jsonfile.read() + jsonfile.close() + jsonfile = open(langfile,"w") + for transblock in tpattern.findall(text): + text = text.replace(transblock, '') + lines = text.split('\n') + + # Parse PO files + for pofile in sorted(glob.glob(podir + '/*.po'), reverse = True): + lang = pofile[:-3].rsplit('/',1)[1] + pofilename = pofile + po = polib.pofile(pofilename) + for entry in po.translated_entries(): + for line in lines: + if entry.msgid in line: + indent = line.replace('\"' + entry.msgctxt + '\": \"' + entry.msgid + '\",', '') + origandtranslated = line + '\n' + indent + '\"' + entry.msgctxt + '[' + lang + ']' + '\": \"' + entry.msgstr + '\",' + text = text.replace(line, origandtranslated) + + jsonfile.write(text) + jsonfile.close() + -- cgit v1.2.1