save just a final dictionary to translations.py instead of tuples

We can do that since In our workflow we won't be converting translations.py to .po files, that way it will be much shorter as it's containing only translated strings and doesn't have strings sources.
This commit is contained in:
Andrej730
2024-01-19 13:57:19 +05:00
parent 3545a5ad4a
commit b824ab4c4f
+14 -49
View File
@@ -171,8 +171,10 @@ def update_translations_from_po(po_directory: Path, translations_module: Path):
msg.translations[language] = msgstr msg.translations[language] = msgstr
# load data from .po files # load data from .po files
langs = set()
for po_file_path in po_directory.glob("**/*.po"): for po_file_path in po_directory.glob("**/*.po"):
lang = po_file_path.stem lang = po_file_path.stem
langs.add(lang)
with open(po_file_path, "r") as po_file: with open(po_file_path, "r") as po_file:
current_chunk = [] current_chunk = []
for line in po_file: for line in po_file:
@@ -182,59 +184,22 @@ def update_translations_from_po(po_directory: Path, translations_module: Path):
current_chunk = [] current_chunk = []
# generate translations.py file # generate translations.py file
# code originating from Blender's bl_i18n_utils/utils.py
# https://projects.blender.org/blender/blender/src/branch/main/scripts/modules/bl_i18n_utils/utils.py
ret = [
"# Tuple of tuples:",
"# ((msgctxt, msgid), (sources, gen_comments), (lang, translation, (is_fuzzy, comments)), ...)",
"translations_tuple = (",
]
tab = " " tab = " "
default_context = "*" default_context = "*"
for msgid, msg in translation_data.items(): ret = ["translations_dict = {"]
# Key (context + msgid).
msgctxt = msg.msgctxt
if not msgctxt:
msgctxt = default_context
ret.append(tab + '(({}, "{}"),'.format(f'"{msgctxt}"' if msgctxt else "None", msgid))
# Common comments (mostly sources!).
sources = []
if not (sources):
ret.append(tab + " ((), ()),")
else:
if len(sources) > 1:
# make sure sources are unique but keep the order
sources = list(dict.fromkeys(msg.sources))
ret.append(tab + f' (("{sources[0]}",')
ret += [tab + f' "{s}",' for s in sources[1:-1]]
ret.append(tab + f' "{sources[-1]}"),')
else:
ret.append(tab + " ((" + (f'"{sources[0]}",' if sources else "") + "),")
# All languages for lang in langs:
for lang, msgstr in msg.translations.items(): ret.append(f'{tab}"{lang}": {{')
is_fuzzy = False for msgid, msg in translation_data.items():
# Language code and translation. if (msgstr := msg.translations[lang]) in (None, ""):
ret.append(tab + ' ("' + lang + f'", "{msgstr}",') continue
# User comments and fuzzy. msgctxt = msg.msgctxt
comments = [] if not msgctxt:
lngspaces = " " * (len(lang) + 6) msgctxt = default_context
ret.append(tab + lngspaces + "(" + ("True" if is_fuzzy else "False") + ",") ret.append(f"{tab*2}({msgctxt!r}, {msgid!r}): {msgstr!r},")
ret[-1] = ret[-1] + " (" + ((f'"{comments[0]}",') if comments else "") + ")))," ret.append(f"{tab}}},")
ret.append(tab + "),") ret.append("}")
ret += [
")",
"",
"translations_dict = {}",
"for msg in translations_tuple:",
tab + "key = msg[0]",
tab + "for lang, trans, (is_fuzzy, comments) in msg[2:]:",
tab * 2 + "if trans and not is_fuzzy:",
tab * 3 + "translations_dict.setdefault(lang, {})[key] = trans",
"",
]
with open(translations_module / "translations.py", "w") as fo: with open(translations_module / "translations.py", "w") as fo:
fo.write("\n".join(ret)) fo.write("\n".join(ret))