-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added language translation json schema merging script
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import shutil | ||
import re | ||
|
||
""" | ||
This script takes in a DH schema.json file as a reference, and a | ||
schema.raw_translation.json file mirror of the schema.json file, | ||
containing translated text in a particular language - which is only valid | ||
for an allowed "keys" set of fields below. The script merges the | ||
reference and translated files, ensuring all reference fields are present, | ||
and outputs a new "adjusted.json" schema that is the translation. | ||
Note that there are a few sections in schema.json that should be copied | ||
with no translation content, namely "types" and "prefixes". | ||
""" | ||
|
||
re_keyval = re.compile('^(\s*"([^"]+)":)(.+)$'); | ||
# So group 1 = leading space plus key plus quotes and colon; group 2 = key; group 3 = remainder | ||
|
||
|
||
with open('schema.raw_translation.json') as translated, \ | ||
open('adjusted.json', 'w') as new, \ | ||
open('../../schema.json') as schema: | ||
|
||
schema_lines = schema.readlines(); | ||
# Keys which translation is allowed to revise | ||
keys = set(["name", "title", "text", "description", "slot_group", "comments", "value"]); | ||
|
||
counter = 0; | ||
|
||
for line in translated: | ||
schema_line = schema_lines[counter]; | ||
schema_binding = re_keyval.search(schema_line); | ||
if schema_binding: | ||
translation = re_keyval.search(line); | ||
key = schema_binding.group(2); | ||
if key in keys: # only some keys can be translated | ||
line = schema_binding.group(1) + translation.group(3) + '\n'; | ||
else: | ||
line = schema_line; | ||
|
||
counter +=1; | ||
new.write(line) | ||
|
||
#shutil.move('adjusted.json', 'schema.json') |