Update import script to insert new translations

This commit is contained in:
Oliver Hamlet
2024-07-03 20:57:36 +01:00
parent 9cc458d675
commit 6e609d2dd1
+61 -10
View File
@@ -9,13 +9,33 @@ import os
import yaml
messages = {}
def get_expected_languages(messages_for_anchor):
expected_langs = list(messages_for_anchor.keys())
# Move en to be the first language.
en_index = expected_langs.index('en')
en = expected_langs.pop(en_index)
expected_langs.insert(0, en)
return expected_langs
def get_yaml_string(raw_message_text):
return yaml.dump(raw_message_text, width=math.inf, allow_unicode=True, default_style="'").strip()
messages_by_lang = {}
for f in os.scandir('translations'):
match = re.match(r'messages\.([^\.]+)\.yaml', f.name)
if match:
lang = match.group(1)
with open(f.path, encoding='utf8') as input:
messages[lang] = yaml.safe_load(input)
messages_by_lang[lang] = yaml.safe_load(input)
messages = {}
for lang in messages_by_lang:
for anchor in messages_by_lang[lang]:
if not anchor in messages:
messages[anchor] = {}
messages[anchor][lang] = messages_by_lang[lang][anchor]
lines = []
with open('prelude.yaml', encoding='utf8') as input:
@@ -23,27 +43,58 @@ with open('prelude.yaml', encoding='utf8') as input:
anchor = None
lang = None
for index, line in enumerate(lines):
match = re.search(r' &([^\s]+)$', line)
expected_langs = []
index = 0
while index < len(lines):
match = re.search(r' &([^\s]+)$', lines[index])
if match != None:
if expected_langs:
# Expected more languages than were found in the last anchor,
# need to go back and append the remaining expected languages.
print('Expected more languages', anchor, expected_langs)
exit(1)
anchor = match.group(1)
expected_langs = get_expected_languages(messages[anchor])
lang = None
text = None
index += 1
continue
match = re.search(r'- lang: ([a-zA-Z_]+)', line)
match = re.search(r'- lang: ([a-zA-Z_]+)', lines[index])
if match != None:
lang = match.group(1)
text = None
if lang == expected_langs[0]:
expected_langs.pop(0)
else:
# Insert the expected language above this one.
match = re.search(r'(\s+)- lang:', lines[index])
whitespace = match.group(1)
text = get_yaml_string(messages[anchor][expected_langs[0]])
new_lang_line = f'{whitespace}- lang: {expected_langs[0]}\n'
new_text_line = f'{whitespace} text: {text}\n'
lines.insert(index, new_text_line)
lines.insert(index, new_lang_line)
expected_langs.pop(0)
expected_langs.pop(0)
index += 2
index += 1
continue
match = re.search(r' text: \'(.+)\'', line)
match = re.search(r' text: \'(.+)\'', lines[index])
if match != None and anchor and lang:
# Replace line with a new line for this message
message = yaml.dump(messages[lang][anchor], width=math.inf, allow_unicode=True, default_style="'")
text = get_yaml_string(messages[anchor][lang])
lines[index] = re.sub(r' text: \'.+\'', f' text: {message.strip()}', line)
lines[index] = re.sub(r' text: \'.+\'', f' text: {text}', lines[index])
index += 1
with open('prelude.yaml', mode='w', encoding='utf8') as output:
output.writelines(lines)