bl_i18n_utils - bugs saving translations to py files and finding po strings if context is not present in the file

4 changes in this PR:

1) Fixed bug with saving translations to py files - saving sources and comments it didn't add `repr` for the saved strings which was producing unexpected special characters.

Then these special characters sometimes made `translations.py` invalid (e.g., it was failing to load with some `\u` combinations) or was producing invalid characters in .po files created from the `translations.py`.

2) `find_best_messages_matches` used to fail when current message context wasn't found in the .po file. E.g. there were no messages with context "View3D", "Operator", ... and you would try to edit translation for the element that uses that context. I've added a fallback to empty sets to make sure it won't fail and just return the valid result that there are no matches.

3) added minor typing hint for easier code navigation.

4) added print to make it clear what data is printed.

Pull Request: https://projects.blender.org/blender/blender/pulls/116934
This commit is contained in:
Andrej730 2024-02-06 16:58:47 +01:00 committed by Bastien Montagne
parent 6b6e57c98c
commit a8e14af167
2 changed files with 7 additions and 2 deletions

View File

@ -11,6 +11,7 @@ import os
import re
import sys
import glob
from pathlib import PurePath
# XXX Relative import does not work here when used from Blender...
from bl_i18n_utils import settings as settings_i18n, utils
@ -664,6 +665,7 @@ def dump_py_messages_from_files(msgs, reports, files, settings):
root_node = ast.parse(filedata.read(), fp, 'exec')
fp_rel = make_rel(fp)
fp_rel = PurePath(fp_rel).as_posix()
for node in ast.walk(root_node):
if type(node) == ast.Call:

View File

@ -15,6 +15,7 @@ from bl_i18n_utils import (
settings,
utils_rtl,
)
from typing import Dict
##### Misc Utils #####
@ -737,6 +738,7 @@ class I18nMessages:
self._reverse_cache = None
if rebuild_now:
src_to_msg, ctxt_to_msg, msgid_to_msg, msgstr_to_msg = {}, {}, {}, {}
ctxt_to_msg.setdefault(self.settings.DEFAULT_CONTEXT, set())
for key, msg in self.msgs.items():
if msg.is_commented:
continue
@ -799,7 +801,7 @@ class I18nMessages:
rlbl = getattr(msgs, msgmap["rna_label"]["msgstr"])
# print("rna label: " + rlbl, rlbl in msgid_to_msg, rlbl in msgstr_to_msg)
if rlbl:
k = ctxt_to_msg[rna_ctxt].copy()
k = ctxt_to_msg.get(rna_ctxt, set()).copy()
if k and rlbl in msgid_to_msg:
k &= msgid_to_msg[rlbl]
elif k and rlbl in msgstr_to_msg:
@ -1253,7 +1255,7 @@ class I18n:
def __init__(self, kind=None, src=None, langs=set(), settings=settings):
self.settings = settings
self.trans = {}
self.trans: Dict[str, I18nMessages] = {}
self.src = {} # Should have the same keys as self.trans (plus PARSER_PY_ID for py file)!
self.dst = self._dst # A callable that transforms src_path into dst_path!
if kind and src:
@ -1485,6 +1487,7 @@ class I18n:
if langs:
translations &= langs
translations = [('"' + lng + '"', " " * (len(lng) + 6), self.trans[lng]) for lng in sorted(translations)]
print("Translated keys saved to .py file:")
print(*(k for k in keys.keys()))
for key in keys.keys():
if ref.msgs[key].is_commented: