Merge pull request #2587 from aussig/feature/issue-2586

Improvements to localisation - issue #2586
This commit is contained in:
David Sangrey authored and GitHub committed 2026-01-17 14:51:48 -05:00
commit 1898be4b58
2 files changed
+6 -33

No files matched your search

+1 -31
View File
@@ -83,7 +83,7 @@ class Translations:
:param lang: The language to translate to, defaults to the preferred language
"""
available = self.available()
available: set[str] = self.available()
available.add(Translations.FALLBACK)
if not lang:
# Choose the default language
@@ -331,33 +331,3 @@ class _Locale:
# singletons
Locale = _Locale()
translations = Translations()
# generate template strings file - like xgettext
# parsing is limited - only single ' or " delimited strings, and only one string per line
if __name__ == "__main__":
regexp = re.compile(r'''_\([ur]?(['"])(((?<!\\)\\\1|.)+?)\1\)[^#]*(#.+)?''') # match a single line python literal
seen: dict[str, str] = {}
plugin_dir = pathlib.Path('plugins')
for f in (
sorted(x for x in listdir('.') if x.endswith('.py')) +
sorted(plugin_dir.glob('*.py')) if plugin_dir.is_dir() else []
):
with open(f, encoding='utf-8') as h:
lineno = 0
for line in h:
lineno += 1
match = regexp.search(line)
if match and not seen.get(match.group(2)): # only record first commented instance of a string
seen[match.group(2)] = (
(match.group(4) and (match.group(4)[1:].strip()) + '. ' or '') + f'[{pathlib.Path(f).name}]'
)
if seen:
target_path = LOCALISATION_DIR / 'en.template.new'
target_path.parent.mkdir(parents=True, exist_ok=True)
with open(target_path, 'w', encoding='utf-8') as target_file:
target_file.write(f'/* Language name */\n"{LANGUAGE_ID}" = "English";\n\n')
for thing in sorted(seen, key=str.lower):
if seen[thing]:
target_file.write(f'/* {seen[thing]} */\n')
target_file.write(f'"{thing}" = "{thing}";\n\n')
+5 -2
View File
@@ -60,7 +60,7 @@ COMMENT_OWN_LINE_RE is for a comment on its own line.
The difference is necessary in order to tell if a 'above' LANG comment is for
its own line (SAME_LINE), or meant to be for this following line (OWN_LINE).
"""
COMMENT_SAME_LINE_RE = re.compile(r"^.*?(#.*)$")
COMMENT_SAME_LINE_RE = re.compile(r"^.*(#.*)$")
COMMENT_OWN_LINE_RE = re.compile(r"^\s*?(#.*)$")
@@ -245,6 +245,8 @@ def generate_lang_template(data: dict[pathlib.Path, list[ast.Call]]) -> str:
entries: list[LangEntry] = []
for path, calls in data.items():
for c in calls:
if getattr(c, "comment") == "Ignore":
continue
entries.append(
LangEntry(
[FileLocation.from_call(path, c)],
@@ -259,7 +261,8 @@ def generate_lang_template(data: dict[pathlib.Path, list[ast.Call]]) -> str:
"""
print(f"Done Deduping entries {len(entries)=} {len(deduped)=}", file=sys.stderr)
for entry in deduped:
for entry in sorted(deduped, key=lambda e: e.string.lower()):
if len(entry.comments) != len(entry.locations):
raise ValueError("Mismatch: 'comments' and 'locations' must have the same length.")