Skip to content

Commit 0e893c8

Browse files
fix(i18n): stop flagging literal "% of" labels as python-format
1 parent 5e6525e commit 0e893c8

37 files changed

Lines changed: 624 additions & 288 deletions

File tree

‎scripts/translations/babel_update.sh‎

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -108,33 +108,12 @@ do
108108
fi
109109
done
110110

111-
# A few UI labels ("% calculation" etc.) parse as accidentally-valid
112-
# %-format directives (a space flag plus a conversion character), so
113-
# babel auto-flags them python-format on every write and msgfmt then
114-
# fatals on their translations. The app never %-formats them; strip the
115-
# flag AFTER the update pass (babel re-adds it during the update, so
111+
# UI text with a literal percent sign ("% of total", "% calculation", ...)
112+
# parses as accidentally-valid %-format directives (a space flag plus a
113+
# conversion character), so babel auto-flags it python-format on every write
114+
# and msgfmt then fatals on its translations. The app never %-formats it;
115+
# strip the flag AFTER the update pass (babel re-adds it during the update, so
116116
# this must run last). Line-targeted so canonical wrapping is untouched.
117-
python3 - <<'PYEOF'
118-
import glob
119-
120-
TARGETS = {'msgid "% calculation"\n', 'msgid "% of parent"\n', 'msgid "% of total"\n'}
121-
paths = ["superset/translations/messages.pot"] + sorted(
122-
glob.glob("superset/translations/*/LC_MESSAGES/messages.po")
123-
)
124-
for path in paths:
125-
lines = open(path, encoding="utf-8").readlines()
126-
changed = False
127-
for i, line in enumerate(lines):
128-
if line in TARGETS and i > 0 and lines[i - 1].startswith("#,"):
129-
tokens = [t.strip() for t in lines[i - 1][2:].split(",")]
130-
if "python-format" in tokens:
131-
tokens = [t for t in tokens if t != "python-format"]
132-
if "no-python-format" not in tokens:
133-
tokens.append("no-python-format")
134-
lines[i - 1] = "#, " + ", ".join(tokens) + "\n"
135-
changed = True
136-
if changed:
137-
open(path, "w", encoding="utf-8").writelines(lines)
138-
PYEOF
117+
python scripts/translations/literal_percent.py || exit 1
139118

140119
cd $CURRENT_DIR

‎scripts/translations/backfill_po.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -810,11 +810,6 @@ def _process_batches(
810810
return translated_count, failed_count
811811

812812

813-
# Mirrors the post-processing at the end of babel_update.sh: Babel tags these
814-
# msgids python-format because of the bare ``%``, but they are literal labels.
815-
_PERCENT_LABEL_MSGIDS = {"% calculation", "% of parent", "% of total"}
816-
817-
818813
def normalize_catalog(po_path: Path) -> None:
819814
"""Re-serialize a catalog in the canonical form ``babel_update.sh`` writes.
820815
@@ -827,11 +822,17 @@ def normalize_catalog(po_path: Path) -> None:
827822
"""
828823
from babel.messages.pofile import read_po, write_po
829824

825+
# A sibling script; importable because this file runs as a script.
826+
from literal_percent import is_literal_percent
827+
830828
lang = po_path.parent.parent.name
831829
with open(po_path, "rb") as f:
832830
catalog = read_po(f, locale=lang)
833831
for message in catalog:
834-
if message.id in _PERCENT_LABEL_MSGIDS and "python-format" in message.flags:
832+
# Mirrors the post-processing at the end of babel_update.sh: Babel
833+
# tags msgids like "% of total" python-format, but the ``%`` is literal.
834+
msgids = message.id if isinstance(message.id, (list, tuple)) else [message.id]
835+
if "python-format" in message.flags and is_literal_percent(*msgids):
835836
message.flags.discard("python-format")
836837
message.flags.add("no-python-format")
837838
with open(po_path, "wb") as f:
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/usr/bin/env python3
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
"""Drop the python-format flag from messages whose ``%`` is literal text.
19+
20+
Babel flags a message ``python-format`` whenever its msgid contains something
21+
that parses as a %-format directive. UI text such as "% of total" or
22+
"% calculation" parses by accident: ``% o`` and ``% c`` read as a space flag
23+
plus a conversion character. The app never %-formats these strings, and msgfmt
24+
rejects any translation that does not repeat the accidental directive. This
25+
swaps ``python-format`` for ``no-python-format`` on those entries.
26+
27+
Babel re-adds the flag every time it writes a catalog, so ``babel_update.sh``
28+
runs this after its ``pybabel update`` pass, and ``backfill_po.py`` applies
29+
``is_literal_percent`` when it re-serializes a catalog. Only ``#,`` flag lines
30+
are rewritten, so the canonical wrapping of the files is untouched.
31+
32+
Usage:
33+
python scripts/translations/literal_percent.py [PATH ...]
34+
# PATH defaults to messages.pot and every language's messages.po
35+
"""
36+
37+
from __future__ import annotations
38+
39+
import re
40+
import sys
41+
from pathlib import Path
42+
43+
from babel.messages.catalog import PYTHON_FORMAT
44+
from babel.messages.pofile import unescape
45+
46+
TRANSLATIONS_DIR: Path = (
47+
Path(__file__).parent.parent.parent / "superset" / "translations"
48+
)
49+
50+
# A percent sign followed by a space and a word, as in "% of" or "50% off".
51+
# A real space-flag directive ("% d") is never followed by more letters.
52+
_LITERAL_PERCENT = re.compile(r"% (?=[A-Za-z]{2})")
53+
54+
55+
def is_literal_percent(*msgids: str) -> bool:
56+
"""Return True if Babel sees %-format directives only in literal ``% word`` text.
57+
58+
Pass the msgid and, for plural entries, the msgid_plural. A message that
59+
also has a real placeholder (``%(name)s``, ``%s``) keeps its flag.
60+
"""
61+
return any(PYTHON_FORMAT.search(msgid) for msgid in msgids) and not any(
62+
PYTHON_FORMAT.search(_LITERAL_PERCENT.sub(" ", msgid)) for msgid in msgids
63+
)
64+
65+
66+
def _read_field(lines: list[str], i: int, keyword: str) -> tuple[str | None, int]:
67+
"""Read a possibly wrapped ``keyword "..."`` field starting at line ``i``.
68+
69+
Returns the unescaped value (None if line ``i`` is not that field) and the
70+
index of the first line after it.
71+
"""
72+
if i >= len(lines) or not lines[i].startswith(f'{keyword} "'):
73+
return None, i
74+
parts = [lines[i][len(keyword) :].strip()]
75+
i += 1
76+
while i < len(lines) and lines[i].startswith('"'):
77+
parts.append(lines[i].strip())
78+
i += 1
79+
return "".join(unescape(part) for part in parts), i
80+
81+
82+
def fix_flags(path: Path) -> bool:
83+
"""Rewrite the flags of literal-percent entries in ``path``.
84+
85+
Returns True if the file changed.
86+
"""
87+
lines = path.read_text(encoding="utf-8").splitlines(keepends=True)
88+
changed = False
89+
for i, line in enumerate(lines):
90+
if not line.startswith("#,"):
91+
continue
92+
flags = [flag.strip() for flag in line[2:].split(",")]
93+
if "python-format" not in flags:
94+
continue
95+
j = i + 1
96+
while j < len(lines) and lines[j].startswith("#|"):
97+
j += 1
98+
_, j = _read_field(lines, j, "msgctxt")
99+
msgid, j = _read_field(lines, j, "msgid")
100+
if msgid is None:
101+
continue
102+
msgids = [msgid]
103+
plural, j = _read_field(lines, j, "msgid_plural")
104+
if plural is not None:
105+
msgids.append(plural)
106+
if not is_literal_percent(*msgids):
107+
continue
108+
flags = [flag for flag in flags if flag != "python-format"]
109+
if "no-python-format" not in flags:
110+
flags.append("no-python-format")
111+
lines[i] = "#, " + ", ".join(flags) + "\n"
112+
changed = True
113+
if changed:
114+
path.write_text("".join(lines), encoding="utf-8", newline="\n")
115+
return changed
116+
117+
118+
def main(argv: list[str]) -> int:
119+
paths = [Path(arg) for arg in argv[1:]] or [
120+
TRANSLATIONS_DIR / "messages.pot",
121+
*sorted(TRANSLATIONS_DIR.glob("*/LC_MESSAGES/messages.po")),
122+
]
123+
for path in paths:
124+
if fix_flags(path):
125+
print(f"Cleared python-format on literal-percent entries in {path}")
126+
return 0
127+
128+
129+
if __name__ == "__main__":
130+
sys.exit(main(sys.argv))

‎superset/translations/ar/LC_MESSAGES/messages.po‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,22 +238,22 @@ msgid "% calculation"
238238
msgstr "حساب ٪"
239239

240240
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
241-
#, fuzzy, python-format
241+
#, fuzzy, no-python-format
242242
msgid "% of"
243243
msgstr "% من"
244244

245245
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
246-
#, fuzzy, python-format
246+
#, fuzzy, no-python-format
247247
msgid "% of column"
248248
msgstr "% من العمود"
249249

250250
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
251-
#, fuzzy, python-format
251+
#, fuzzy, no-python-format
252252
msgid "% of column total"
253253
msgstr "% من إجمالي العمود"
254254

255255
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
256-
#, fuzzy, python-format
256+
#, fuzzy, no-python-format
257257
msgid "% of grand total"
258258
msgstr "% من الإجمالي الكلي"
259259

@@ -264,7 +264,7 @@ msgid "% of parent"
264264
msgstr "% من الأصل"
265265

266266
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
267-
#, fuzzy, python-format
267+
#, fuzzy, no-python-format
268268
msgid "% of row total"
269269
msgstr "% من إجمالي الصف"
270270

@@ -14452,7 +14452,7 @@ msgid "Optional warning about use of this metric"
1445214452
msgstr "تحذير اختياري حول استخدام هذا المقياس"
1445314453

1445414454
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
14455-
#, fuzzy, python-format
14455+
#, fuzzy, no-python-format
1445614456
msgid ""
1445714457
"Optional. When set together with Low color, Mid color, and High color "
1445814458
"below, colors diverge from Mid color at this value toward Low color below"
@@ -24300,7 +24300,7 @@ msgid "Value transform"
2430024300
msgstr "تحويل القيمة"
2430124301

2430224302
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
24303-
#, fuzzy, python-format
24303+
#, fuzzy, no-python-format
2430424304
msgid ""
2430524305
"Value: type the exact numbers used for coloring below. % of column is "
2430624306
"unavailable with Server pagination enabled, since each page would compute"
@@ -24313,7 +24313,7 @@ msgstr ""
2431324313
"التلقائي أثناء تفعيل ترقيم الصفحات من الخادم."
2431424314

2431524315
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
24316-
#, fuzzy, python-format
24316+
#, fuzzy, no-python-format
2431724317
msgid ""
2431824318
"Value: type the exact numbers used for coloring below. % of column: type "
2431924319
"a percentage of the column maximum or sum selected below, so the rule "

‎superset/translations/ca/LC_MESSAGES/messages.po‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,22 +234,22 @@ msgid "% calculation"
234234
msgstr "% càlcul"
235235

236236
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
237-
#, fuzzy, python-format
237+
#, fuzzy, no-python-format
238238
msgid "% of"
239239
msgstr "% de"
240240

241241
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
242-
#, fuzzy, python-format
242+
#, fuzzy, no-python-format
243243
msgid "% of column"
244244
msgstr "% de la columna"
245245

246246
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
247-
#, fuzzy, python-format
247+
#, fuzzy, no-python-format
248248
msgid "% of column total"
249249
msgstr "% del total de la columna"
250250

251251
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
252-
#, fuzzy, python-format
252+
#, fuzzy, no-python-format
253253
msgid "% of grand total"
254254
msgstr "% del total general"
255255

@@ -258,7 +258,7 @@ msgid "% of parent"
258258
msgstr "%% del pare"
259259

260260
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
261-
#, fuzzy, python-format
261+
#, fuzzy, no-python-format
262262
msgid "% of row total"
263263
msgstr "% del total de la fila"
264264

@@ -14109,7 +14109,7 @@ msgid "Optional warning about use of this metric"
1410914109
msgstr "Advertència opcional sobre l'ús d'aquesta mètrica"
1411014110

1411114111
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
14112-
#, fuzzy, python-format
14112+
#, fuzzy, no-python-format
1411314113
msgid ""
1411414114
"Optional. When set together with Low color, Mid color, and High color "
1411514115
"below, colors diverge from Mid color at this value toward Low color below"
@@ -23907,7 +23907,7 @@ msgid "Value transform"
2390723907
msgstr "Transformació del valor"
2390823908

2390923909
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
23910-
#, fuzzy, python-format
23910+
#, fuzzy, no-python-format
2391123911
msgid ""
2391223912
"Value: type the exact numbers used for coloring below. % of column is "
2391323913
"unavailable with Server pagination enabled, since each page would compute"
@@ -23921,7 +23921,7 @@ msgstr ""
2392123921
"dades mentre la paginació del servidor està activada."
2392223922

2392323923
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
23924-
#, fuzzy, python-format
23924+
#, fuzzy, no-python-format
2392523925
msgid ""
2392623926
"Value: type the exact numbers used for coloring below. % of column: type "
2392723927
"a percentage of the column maximum or sum selected below, so the rule "

‎superset/translations/cs/LC_MESSAGES/messages.po‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,22 +226,22 @@ msgid "% calculation"
226226
msgstr "Výpočet v %"
227227

228228
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
229-
#, fuzzy, python-format
229+
#, fuzzy, no-python-format
230230
msgid "% of"
231231
msgstr "% z"
232232

233233
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
234-
#, fuzzy, python-format
234+
#, fuzzy, no-python-format
235235
msgid "% of column"
236236
msgstr "% sloupce"
237237

238238
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
239-
#, fuzzy, python-format
239+
#, fuzzy, no-python-format
240240
msgid "% of column total"
241241
msgstr "% součtu sloupce"
242242

243243
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
244-
#, fuzzy, python-format
244+
#, fuzzy, no-python-format
245245
msgid "% of grand total"
246246
msgstr "% celkového součtu"
247247

@@ -250,7 +250,7 @@ msgid "% of parent"
250250
msgstr "% z nadřazené položky"
251251

252252
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
253-
#, fuzzy, python-format
253+
#, fuzzy, no-python-format
254254
msgid "% of row total"
255255
msgstr "% součtu řádku"
256256

@@ -13598,7 +13598,7 @@ msgid "Optional warning about use of this metric"
1359813598
msgstr "Volitelné varování o použití této metriky"
1359913599

1360013600
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
13601-
#, fuzzy, python-format
13601+
#, fuzzy, no-python-format
1360213602
msgid ""
1360313603
"Optional. When set together with Low color, Mid color, and High color "
1360413604
"below, colors diverge from Mid color at this value toward Low color below"
@@ -23002,7 +23002,7 @@ msgid "Value transform"
2300223002
msgstr "Transformace hodnoty"
2300323003

2300423004
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
23005-
#, fuzzy, python-format
23005+
#, fuzzy, no-python-format
2300623006
msgid ""
2300723007
"Value: type the exact numbers used for coloring below. % of column is "
2300823008
"unavailable with Server pagination enabled, since each page would compute"
@@ -23016,7 +23016,7 @@ msgstr ""
2301623016
"serveru automatický rozsah dat."
2301723017

2301823018
# Machine-translated via backfill_po.py (claude-sonnet-5-5) [refs: de]
23019-
#, fuzzy, python-format
23019+
#, fuzzy, no-python-format
2302023020
msgid ""
2302123021
"Value: type the exact numbers used for coloring below. % of column: type "
2302223022
"a percentage of the column maximum or sum selected below, so the rule "

0 commit comments

Comments
 (0)