aboutsummaryrefslogtreecommitdiffstats
path: root/plasma_json/po2json.py
diff options
context:
space:
mode:
authorYuri Chornoivan <yurchor@ukr.net>2021-01-09 09:33:44 +0200
committerYuri Chornoivan <yurchor@ukr.net>2021-01-09 09:33:44 +0200
commit1d3f14d1b60448aa3fb368e1b457d31598ab5d33 (patch)
tree11224b8c78669dd3e7d0837a44b005b082b39f87 /plasma_json/po2json.py
parent7e581c99ef866678c493ae608c527d86024a0a1a (diff)
downloadtools-1d3f14d1b60448aa3fb368e1b457d31598ab5d33.tar
tools-1d3f14d1b60448aa3fb368e1b457d31598ab5d33.tar.gz
tools-1d3f14d1b60448aa3fb368e1b457d31598ab5d33.tar.bz2
tools-1d3f14d1b60448aa3fb368e1b457d31598ab5d33.tar.xz
tools-1d3f14d1b60448aa3fb368e1b457d31598ab5d33.zip
Add Mageia translation for Plasma JSON
Diffstat (limited to 'plasma_json/po2json.py')
-rwxr-xr-xplasma_json/po2json.py120
1 files changed, 120 insertions, 0 deletions
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 <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 <EMAIL@ADDRESS>',
+ 'Language-Team': 'LANGUAGE <LL@li.org>',
+ '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()
+