Skip to content

Commit

Permalink
Merge normalize and reconvert l10n scripts, keep author info, format …
Browse files Browse the repository at this point in the history
…the files properly, remove tabs from data
  • Loading branch information
Dune-jr committed Jan 6, 2019
1 parent d215c73 commit 5090c73
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
11 changes: 0 additions & 11 deletions scripts/normalize_po.py

This file was deleted.

51 changes: 45 additions & 6 deletions scripts/reconvert_l10n.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import json
import polib
import json
import sys

JSON_KEY_TRANSL="translated strings"
JSON_KEY_AUTHOR="authors"
JSON_KEY_MODIFY="modified by"
JSON_KEY_OR="or"
JSON_KEY_TR="tr"
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())
translations = []

Expand All @@ -19,18 +23,53 @@ def reconvert(filename):
t_entry[JSON_KEY_CO] = entry.msgctxt
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(
result,
open(filename + '.json', 'w', encoding='utf-8'),
open(json_filename, 'w', encoding='utf-8'),
ensure_ascii=False,
indent="\t",
separators=(',', ': '),
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__':
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:]:
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)

0 comments on commit 5090c73

Please sign in to comment.