Merge normalize and reconvert l10n scripts, keep author info, format the files properly, remove tabs from data

This commit is contained in:
Jordy Ruiz 2019-01-06 13:42:37 +01:00
parent d215c73206
commit 5090c73250
2 changed files with 45 additions and 17 deletions

View file

@ -1,11 +0,0 @@
import polib
import sys
if __name__ == '__main__':
for filename in sys.argv[1:]:
po = polib.pofile(open(filename).read())
entries = list(sorted(sorted(po, key=lambda x: x.msgctxt or ""), key=lambda x: x.msgid))
po.clear()
for entry in entries:
po.append(entry)
po.save(filename)

View file

@ -1,12 +1,16 @@
import json
import polib import polib
import json
import sys
JSON_KEY_TRANSL="translated strings" JSON_KEY_TRANSL="translated strings"
JSON_KEY_AUTHOR="authors"
JSON_KEY_MODIFY="modified by"
JSON_KEY_OR="or" JSON_KEY_OR="or"
JSON_KEY_TR="tr" JSON_KEY_TR="tr"
JSON_KEY_CO="context" JSON_KEY_CO="context"
def reconvert(filename): def reconvert(filename, json_filename):
"""Converts a l10n file.po into a file.json, keeping author info from a previous file.json if possible"""
po = polib.pofile(open(filename, encoding='utf-8').read()) po = polib.pofile(open(filename, encoding='utf-8').read())
translations = [] translations = []
@ -19,18 +23,53 @@ def reconvert(filename):
t_entry[JSON_KEY_CO] = entry.msgctxt t_entry[JSON_KEY_CO] = entry.msgctxt
translations.append(t_entry) translations.append(t_entry)
result = {JSON_KEY_TRANSL: translations} try:
previous_l10n = json.load(open(json_filename, encoding='utf-8'), strict=False)
# remove tabs from data
authors = previous_l10n[JSON_KEY_AUTHOR]
for i, value in enumerate(authors[JSON_KEY_MODIFY]):
authors[JSON_KEY_MODIFY][i] = value.replace("\t", " ")
result = {JSON_KEY_AUTHOR: authors, JSON_KEY_TRANSL: translations}
print(" extracted author info from " + json_filename)
except IOError:
result = {JSON_KEY_AUTHOR: "", JSON_KEY_TRANSL: translations}
print(" failed to open " + json_filename + ", skipping author info")
json.dump( json.dump(
result, result,
open(filename + '.json', 'w', encoding='utf-8'), open(json_filename, 'w', encoding='utf-8'),
ensure_ascii=False, ensure_ascii=False,
indent="\t", indent="\t",
separators=(',', ': '), separators=(',', ': '),
sort_keys=True, sort_keys=True,
) )
def normalize(filename):
"""Normalizes a l10n file.po for better version controlling"""
po = polib.pofile(open(filename).read())
entries = list(sorted(sorted(po, key=lambda x: x.msgctxt or ""), key=lambda x: x.msgid))
po.clear()
for entry in entries:
po.append(entry)
po.save(filename)
def decrement_indent(filename):
"""decrement indent level of file"""
with open(filename, encoding='utf-8') as f:
sanitized_json=f.read().replace('\n\t', '\n')
with open(filename, 'w', encoding='utf-8') as f:
f.write(sanitized_json)
if __name__ == '__main__': if __name__ == '__main__':
import sys """Normalizes then converts a l10n file.po into a file.json, keeping author info from a previous file.json if possible"""
for filename in sys.argv[1:]: for filename in sys.argv[1:]:
reconvert(filename) assert(len(filename)>len(".po"))
json_filename = filename[:-3]+".json"
normalize(filename)
print(filename + " -> " + filename + " (normalized)")
reconvert(filename, json_filename)
decrement_indent(json_filename)
print(filename + " -> " + json_filename)