libreoffice-online/browser/util/po2json.py
Henry Castro e5c1d0e637 initial rename loleaflet -> browser
Change-Id: Ief7edfe7e3ff3a0a1ec24b4dc106bdeee5d0f8ff
Signed-off-by: Henry Castro <hcastro@collabora.com>
2021-11-10 17:18:56 +01:00

68 lines
1.8 KiB
Python
Executable file

#!/usr/bin/env python3
"""Convert .po to .json."""
import json
import optparse
import os
import sys
import errno
import re
import polib
parser = optparse.OptionParser(usage="usage: %prog [options] pofile...")
parser.add_option("--quiet",
action="store_false",
default=True,
dest="verbose",
help="don't print status messages to stdout")
parser.add_option("-o",
type="string",
default="",
dest="destfile",
help="output file name (if there is exactly one input file)")
(options, args) = parser.parse_args()
if args is None or len(args) == 0:
print("ERROR: you must specify at least one po file to translate")
sys.exit(1)
if options.destfile != '' and len(args) != 1:
print("ERROR: when -o is provided, there has to be exactly 1 input file")
sys.exit(1)
paramFix = re.compile("(\\(([0-9])\\))")
for srcfile in args:
destfile = os.path.splitext(srcfile)[0] + ".json"
if options.destfile != '':
destfile = options.destfile
if options.verbose:
print("INFO: converting %s to %s" % (srcfile, destfile))
xlate_map = {}
po = polib.pofile(srcfile,
autodetect_encoding=False,
encoding="utf-8",
wrapwidth=-1)
for entry in po.translated_entries():
if entry.msgstr == '':
continue
xlate_map[entry.msgid] = entry.msgstr
if not os.path.exists(os.path.dirname(destfile)):
try:
os.makedirs(os.path.dirname(destfile))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
dest = open(destfile, "w")
dest.write(json.dumps(xlate_map, sort_keys=True))
dest.close()