Files
op-packages/luci-theme-carbon/scripts/gen-i18n.py
T

159 lines
6.4 KiB
Python

#!/usr/bin/env python3
"""Extract Carbon's theme strings and compile po/<lang>/carbon.po catalogues.
python3 -I scripts/gen-i18n.py regenerate pot, merge po files, write JS
python3 -I scripts/gen-i18n.py --check CI: fail if anything is stale or untranslated
Sources use t('…') / tr('…') / N_('…') with single-quoted literals. The po files
are standard gettext, so the OpenWrt SDK (luci.mk) also builds them into
luci-i18n-carbon-<lang> packages. Release packages bundle the compiled
catalogues as htdocs/luci-static/carbon/i18n/<lc>.js, which the theme header
loads for the active LuCI language.
"""
from pathlib import Path
import json
import re
import sys
ROOT = Path(__file__).resolve().parent.parent
PO_DIR = ROOT / 'po'
POT = PO_DIR / 'templates' / 'carbon.pot'
OUT_DIR = ROOT / 'htdocs' / 'luci-static' / 'carbon' / 'i18n'
SOURCES = [
'htdocs/luci-static/carbon/carbon.js',
'htdocs/luci-static/resources/menu-carbon.js',
'htdocs/luci-static/resources/view/system/carbon-update.js',
]
MENU_JSON = ['root/usr/share/luci/menu.d/luci-theme-carbon.json']
# Same short names luci.mk uses for lmo files (LUCI_LC_ALIAS).
ALIAS = {'zh_Hans': 'zh-cn', 'zh_Hant': 'zh-tw', 'pt_BR': 'pt-br', 'nb_NO': 'no', 'sr_Latn': 'sr'}
CALL = re.compile(r"(?<![\w$.])(?:C\.)?(?:t|tr|N_)\(\s*'((?:[^'\\\n]|\\.)*)'")
PLACEHOLDER = re.compile(r'%[sd]')
def js_unescape(text):
return re.sub(r'\\(.)', lambda m: {'n': '\n', 't': '\t'}.get(m.group(1), m.group(1)), text)
def extract():
found = {}
# File-only references (like xgettext --add-location=file): editing a source
# does not rewrite every catalogue just because line numbers moved.
for rel in SOURCES:
for line in (ROOT / rel).read_text(encoding='utf-8').splitlines():
for match in CALL.finditer(line):
found.setdefault(js_unescape(match.group(1)), []).append(rel)
for rel in MENU_JSON:
for node in json.loads((ROOT / rel).read_text(encoding='utf-8')).values():
if node.get('title'):
found.setdefault(node['title'], []).append(rel)
return found
def po_escape(text):
return text.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\t', '\\t')
def po_unescape(text):
return re.sub(r'\\(.)', lambda m: {'n': '\n', 't': '\t'}.get(m.group(1), m.group(1)), text)
def parse_po(path):
"""Minimal gettext reader: msgid/msgstr pairs, multi-line strings, no plurals."""
entries, current, field = {}, {}, None
def flush():
if 'msgid' in current and current['msgid'] != '' and 'msgctxt' not in current:
entries[current['msgid']] = current.get('msgstr', '')
current.clear()
for raw in path.read_text(encoding='utf-8').splitlines():
line = raw.strip()
if not line or line.startswith('#'):
if not line and current:
flush()
field = None
continue
match = re.match(r'(msgctxt|msgid|msgstr)\s+"(.*)"$', line)
if match:
if match.group(1) in ('msgid', 'msgctxt') and 'msgstr' in current:
flush()
field = match.group(1)
current[field] = po_unescape(match.group(2))
elif line.startswith('"') and field:
current[field] += po_unescape(line[1:-1])
flush()
return entries
def render_po(msgids, refs, translations, header):
out = [header.rstrip('\n'), '']
for msgid in msgids:
for ref in refs.get(msgid, []):
out.append('#: ' + ref)
out.append(f'msgid "{po_escape(msgid)}"')
out.append(f'msgstr "{po_escape(translations.get(msgid, ""))}"')
out.append('')
return '\n'.join(out)
def po_header(lang=None):
lines = ['msgid ""', 'msgstr ""', '"Content-Type: text/plain; charset=UTF-8\\n"']
if lang:
lines.append(f'"Language: {lang}\\n"')
return '\n'.join(lines) + '\n'
def render_js(lang, msgids, translations):
body = ',\n'.join('\t' + json.dumps(k, ensure_ascii=False) + ': ' + json.dumps(translations[k], ensure_ascii=False)
for k in msgids if translations.get(k))
return ('/* Generated by scripts/gen-i18n.py from po/%s/carbon.po. Do not edit. */\n'
'(window.Carbon = window.Carbon || {}).I18N = {\n%s\n};\n') % (lang, body)
def main(check):
found = extract()
msgids = sorted(found, key=str.casefold)
refs = {k: sorted(set(v)) for k, v in found.items()}
outputs = {POT: render_po(msgids, refs, {}, po_header())}
problems = []
languages = sorted(d.name for d in PO_DIR.iterdir() if d.is_dir() and d.name != 'templates')
for lang in languages:
po = PO_DIR / lang / 'carbon.po'
translations = parse_po(po) if po.exists() else {}
for msgid in msgids:
value = translations.get(msgid, '')
if not value:
problems.append(f'{lang}: untranslated: {msgid!r}')
elif PLACEHOLDER.findall(value) != PLACEHOLDER.findall(msgid):
problems.append(f'{lang}: placeholders differ: {msgid!r} -> {value!r}')
outputs[po] = render_po(msgids, refs, translations, po_header(lang))
outputs[OUT_DIR / (ALIAS.get(lang, lang.lower().replace('_', '-')) + '.js')] = render_js(lang, msgids, translations)
stale = [p for p, text in outputs.items() if not p.exists() or p.read_text(encoding='utf-8') != text]
expected = {p for p in outputs if p.parent == OUT_DIR}
orphans = [p for p in OUT_DIR.glob('*.js') if p not in expected] if OUT_DIR.exists() else []
if check:
for path in stale:
problems.append(f'stale: {path.relative_to(ROOT)} (run scripts/gen-i18n.py)')
for path in orphans:
problems.append(f'orphan catalogue: {path.relative_to(ROOT)}')
for line in problems:
print('::error::' + line if '--github' in sys.argv else line, file=sys.stderr)
return 1 if problems else 0
for path in stale:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(outputs[path], encoding='utf-8')
print('wrote', path.relative_to(ROOT))
for path in orphans:
path.unlink()
print('removed', path.relative_to(ROOT))
for line in problems:
print('warning:', line, file=sys.stderr)
print(f'{len(msgids)} strings, languages: {", ".join(languages) or "none"}')
return 0
if __name__ == '__main__':
sys.exit(main('--check' in sys.argv))