2019-10-30 13:16:11 -05:00
|
|
|
#!/usr/bin/env python3
|
2020-10-10 22:48:52 -05:00
|
|
|
"""Convert .po to .json."""
|
2016-02-08 10:20:05 -06:00
|
|
|
|
|
|
|
import json
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import sys
|
2017-07-10 08:00:40 -05:00
|
|
|
import errno
|
2020-10-10 22:48:52 -05:00
|
|
|
import re
|
|
|
|
import polib
|
2016-02-08 10:20:05 -06:00
|
|
|
|
|
|
|
parser = optparse.OptionParser(usage="usage: %prog [options] pofile...")
|
2020-10-10 22:48:52 -05:00
|
|
|
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)")
|
2016-02-08 10:20:05 -06:00
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2020-10-10 22:48:52 -05:00
|
|
|
if args is None or len(args) == 0:
|
|
|
|
print("ERROR: you must specify at least one po file to translate")
|
|
|
|
sys.exit(1)
|
2016-02-08 10:20:05 -06:00
|
|
|
|
2017-05-05 04:18:26 -05:00
|
|
|
if options.destfile != '' and len(args) != 1:
|
2020-10-10 22:48:52 -05:00
|
|
|
print("ERROR: when -o is provided, there has to be exactly 1 input file")
|
|
|
|
sys.exit(1)
|
2017-05-05 04:18:26 -05:00
|
|
|
|
2016-02-08 10:20:05 -06:00
|
|
|
paramFix = re.compile("(\\(([0-9])\\))")
|
|
|
|
|
|
|
|
for srcfile in args:
|
|
|
|
|
2020-10-10 22:48:52 -05:00
|
|
|
destfile = os.path.splitext(srcfile)[0] + ".json"
|
|
|
|
if options.destfile != '':
|
|
|
|
destfile = options.destfile
|
2017-05-05 04:18:26 -05:00
|
|
|
|
2020-10-10 22:48:52 -05:00
|
|
|
if options.verbose:
|
|
|
|
print("INFO: converting %s to %s" % (srcfile, destfile))
|
2016-02-08 10:20:05 -06:00
|
|
|
|
2020-10-10 22:48:52 -05:00
|
|
|
xlate_map = {}
|
2016-02-08 10:20:05 -06:00
|
|
|
|
2020-10-10 22:48:52 -05:00
|
|
|
po = polib.pofile(srcfile,
|
|
|
|
autodetect_encoding=False,
|
|
|
|
encoding="utf-8",
|
|
|
|
wrapwidth=-1)
|
|
|
|
for entry in po.translated_entries():
|
|
|
|
if entry.msgstr == '':
|
|
|
|
continue
|
2016-02-08 10:20:05 -06:00
|
|
|
|
2020-10-10 22:48:52 -05:00
|
|
|
xlate_map[entry.msgid] = entry.msgstr
|
2016-02-08 10:20:05 -06:00
|
|
|
|
2020-10-10 22:48:52 -05:00
|
|
|
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
|
2017-07-10 08:00:40 -05:00
|
|
|
|
2020-10-10 22:48:52 -05:00
|
|
|
dest = open(destfile, "w")
|
2016-02-08 10:20:05 -06:00
|
|
|
|
2020-10-10 22:48:52 -05:00
|
|
|
dest.write(json.dumps(xlate_map, sort_keys=True))
|
2016-02-08 10:20:05 -06:00
|
|
|
|
2020-10-10 22:48:52 -05:00
|
|
|
dest.close()
|