| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (c) 2008-2026 Andrew Ziem. |
| 3 |
# |
| 4 |
# This work is licensed under the terms of the GNU GPL, version 3 or |
| 5 |
# later. See the COPYING file in the top-level directory. |
| 6 |
|
| 7 |
|
| 8 |
""" |
| 9 |
Tests for translation (.po) files. |
| 10 |
|
| 11 |
These checks cover bugs that ``msgfmt --check`` cannot catch, such as |
| 12 |
NSIS ``${placeholder}`` typos in translations (see commit 104a6211 which |
| 13 |
fixed ``${proname}`` -> ``${prodname}`` in Italian). |
| 14 |
|
| 15 |
For format-string validation run ``make -C po tests``. |
| 16 |
""" |
| 17 |
|
| 18 |
import os |
| 19 |
import re |
| 20 |
import unittest |
| 21 |
|
| 22 |
_PO_DIR = os.path.join(os.path.dirname(__file__), '..', 'po') |
| 23 |
|
| 24 |
# Matches ${name} placeholders used in NSIS strings, e.g. ${prodname}. |
| 25 |
_NSIS_PLACEHOLDER = re.compile(r'\$\{([A-Za-z_][A-Za-z0-9_]*)\}') |
| 26 |
|
| 27 |
|
| 28 |
def _parse_po(path): |
| 29 |
"""Yield (msgctxt, msgid, msgstr) tuples from a .po file. |
| 30 |
|
| 31 |
Minimal stdlib parser; handles multi-line quoted strings and ignores |
| 32 |
fuzzy/obsolete entries. Sufficient for placeholder consistency checks. |
| 33 |
""" |
| 34 |
msgctxt = None |
| 35 |
msgid = None |
| 36 |
msgstr = None |
| 37 |
field = None # which multiline field we are accumulating into |
| 38 |
|
| 39 |
def _unescape(s): |
| 40 |
return s.replace('\\"', '"').replace('\\n', '\n').replace('\\t', '\t') |
| 41 |
|
| 42 |
results = [] |
| 43 |
with open(path, 'r', encoding='utf-8') as f: |
| 44 |
for line in f: |
| 45 |
line = line.rstrip('\n') |
| 46 |
if line.startswith('msgctxt '): |
| 47 |
msgctxt = _unescape(line[len('msgctxt '):].strip().strip('"')) |
| 48 |
field = None |
| 49 |
elif line.startswith('msgid '): |
| 50 |
msgid = _unescape(line[len('msgid '):].strip().strip('"')) |
| 51 |
field = 'msgid' |
| 52 |
elif line.startswith('msgstr '): |
| 53 |
msgstr = _unescape(line[len('msgstr '):].strip().strip('"')) |
| 54 |
field = 'msgstr' |
| 55 |
elif line.startswith('"'): |
| 56 |
text = _unescape(line.strip().strip('"')) |
| 57 |
if field == 'msgid': |
| 58 |
msgid += text |
| 59 |
elif field == 'msgstr': |
| 60 |
msgstr += text |
| 61 |
elif line == '': |
| 62 |
if msgid is not None and msgstr: |
| 63 |
results.append((msgctxt, msgid, msgstr)) |
| 64 |
msgctxt, msgid, msgstr = None, None, None |
| 65 |
field = None |
| 66 |
if msgid is not None and msgstr: |
| 67 |
results.append((msgctxt, msgid, msgstr)) |
| 68 |
return results |
| 69 |
|
| 70 |
|
| 71 |
class TestTranslations(unittest.TestCase): |
| 72 |
"""Validate .po files beyond what msgfmt --check covers.""" |
| 73 |
|
| 74 |
def _po_files(self): |
| 75 |
po_dir = os.path.abspath(_PO_DIR) |
| 76 |
return [os.path.join(po_dir, f) |
| 77 |
for f in os.listdir(po_dir) if f.endswith('.po')] |
| 78 |
|
| 79 |
def test_nsis_placeholders_match(self): |
| 80 |
"""NSIS translations must keep the same ${...} placeholders as msgid. |
| 81 |
|
| 82 |
Regression test for commit 104a6211 (Italian ${proname} typo). |
| 83 |
""" |
| 84 |
failures = [] |
| 85 |
for po_path in self._po_files(): |
| 86 |
for msgctxt, msgid, msgstr in _parse_po(po_path): |
| 87 |
if not msgctxt or not msgctxt.startswith('nsis:'): |
| 88 |
continue |
| 89 |
expected = sorted(_NSIS_PLACEHOLDER.findall(msgid)) |
| 90 |
actual = sorted(_NSIS_PLACEHOLDER.findall(msgstr)) |
| 91 |
if expected != actual: |
| 92 |
failures.append( |
| 93 |
f"{os.path.basename(po_path)}: msgctxt={msgctxt} " |
| 94 |
f"expected={expected} actual={actual}") |
| 95 |
self.assertEqual([], failures, |
| 96 |
"NSIS placeholder mismatch in translations:\n" + |
| 97 |
"\n".join(failures)) |
| 98 |
|
| 99 |
|
| 100 |
if __name__ == '__main__': |
| 101 |
unittest.main() |