pythonize check_messages (merged into check_po)

This commit is contained in:
Patrick Nicolas
2025-08-10 15:15:13 +02:00
parent e744a91488
commit 869911fcd5
3 changed files with 90 additions and 92 deletions
+1 -1
View File
@@ -15,4 +15,4 @@ jobs:
run: | run: |
qtchooser -install qt6 $(which qmake6) qtchooser -install qt6 $(which qmake6)
export QT_SELECT=qt6 export QT_SELECT=qt6
tools/check_messages.sh tools/check_po.py
-56
View File
@@ -1,56 +0,0 @@
#!/bin/bash
cd $(dirname $0)/..
WIVRN_CLIENT_POT=$(mktemp)
WIVRN_DASHBOARD_POT=$(mktemp)
xgettext \
--c++ --from-code=UTF-8 \
--keyword=_:1,1t \
--keyword=_S:1,1t \
--keyword=_cS:1c,2,2t \
--keyword=_F:1,1t \
--output=$WIVRN_CLIENT_POT \
--package-name=WiVRn \
$(find client/ -name "*.cpp" | sort)
xgettext \
--c++ --kde --from-code=UTF-8 \
--keyword=i18n:1 \
--keyword=i18nc:1c,2 \
--keyword=i18np:1,2 \
--keyword=i18ncp:1c,2,3 \
--output=$WIVRN_DASHBOARD_POT \
--package-name=WiVRn-dashboard \
$(find dashboard/ -name "*.qml" -o -name "*.cpp" | sort)
sed -i 's/charset=CHARSET/charset=UTF-8/g' $WIVRN_CLIENT_POT $WIVRN_DASHBOARD_POT
RC=0
for i in locale/*
do
if [ -f $i/wivrn.po ]
then
tools/check_po.py $WIVRN_CLIENT_POT $i/wivrn.po $(basename $i)
if [ $? != 0 ]
then
RC=1
fi
fi
if [ -f $i/wivrn-dashboard.po ]
then
tools/check_po.py $WIVRN_DASHBOARD_POT $i/wivrn-dashboard.po $(basename $i)
if [ $? != 0 ]
then
RC=1
fi
fi
done
rm $WIVRN_CLIENT_POT $WIVRN_DASHBOARD_POT
exit $RC
+89 -35
View File
@@ -3,49 +3,103 @@
import sys import sys
import polib import polib
import argparse import argparse
import os
import pathlib import pathlib
import subprocess
import tempfile
parser = argparse.ArgumentParser( def list_files(base: str, exts: str|list[str]):
prog='check_po.py', if type(exts) == str:
description='Check that all strings are translated in a .po file') exts = [exts]
for root, dirs, files in os.walk(base):
for f in files:
for ext in exts:
if f.endswith(ext):
yield os.path.join(root, f)
parser.add_argument('ref', type=pathlib.Path) def flag(lang: str):
parser.add_argument('file', type=pathlib.Path) # Map language code to a country flag
parser.add_argument('lang', type=str) countries = {
'es': 'es',
'fr': 'fr',
'it': 'it',
'ja': 'jp',
'zh_TW': 'tw',
'pt_BR': 'br'
}
args = parser.parse_args() country = countries.get(lang, lang)
po_ref = polib.pofile(args.ref) res = ''
for i in country.upper():
res += chr(ord(i) + ord('🇦') - ord('A'))
return res
po_file = polib.pofile(args.file) if __name__ == "__main__":
entries = [i.msgid for i in po_file.translated_entries()] root = os.path.dirname(os.path.dirname(__file__))
locale_dir = os.path.join(root, "locale")
cwd = os.getcwd()
# Map language code to a country flag parser = argparse.ArgumentParser(
countries = { prog='check_po.py',
'es': 'es', description='Check that all strings are translated in a .po file')
'fr': 'fr',
'it': 'it',
'ja': 'jp',
'zh_TW': 'tw',
'pt_BR': 'br'
}
if args.lang in countries: parser.add_argument('lang', type=str, nargs="*")
country = countries[args.lang]
else:
country = args.lang
flag = '' args = parser.parse_args()
for i in country.upper():
flag += chr(ord(i) + ord('🇦') - ord('A'))
missing = 0 if len(args.lang) == 0:
for i in po_ref: for d in sorted(os.listdir(locale_dir)):
if not i.obsolete: if os.path.isdir(os.path.join(locale_dir, d)):
if not i.msgid in entries: args.lang.append(d)
missing = missing + 1
print(f"::notice file={args.file}::{flag} Translation for {repr(i.msgid)} is missing")
if missing > 0: with tempfile.TemporaryDirectory() as tmpdir:
print(f"::warning file={args.file}::{flag} {missing} translations missing") client_pot = os.path.join(tmpdir, "client.pot")
sys.exit(1) dashboard_pot = os.path.join(tmpdir, "dashboard.pot")
subprocess.check_call(
["xgettext",
"--c++", "--from-code=UTF-8",
"--keyword=_:1,1t",
"--keyword=_S:1,1t",
"--keyword=_cS:1c,2,2t",
"--keyword=_F:1,1t",
"--output", client_pot,
"--package-name=WiVRn",
] + sorted(list_files(os.path.join(root, "client"), ".cpp"))
)
subprocess.check_call(
["xgettext",
"--c++", "--kde", "--from-code=UTF-8",
"--keyword=i18n:1",
"--keyword=i18nc:1c,2",
"--keyword=i18np:1,2",
"--keyword=i18ncp:1c,2,3",
"--output", dashboard_pot,
"--package-name=WiVRn-dashboard",
] + sorted(list_files(os.path.join(root, "dashboard"), [".cpp", ".qml"]))
)
client_ref = polib.pofile(client_pot)
dashboard_ref = polib.pofile(dashboard_pot)
for lang in args.lang:
for po_ref, po in ((client_ref, os.path.join(locale_dir, lang, "wivrn.po")),
(dashboard_ref, os.path.join(locale_dir, lang, "wivrn-dashboard.po"))):
if not os.path.exists(po):
continue
po_file = polib.pofile(po)
po = os.path.relpath(po, cwd)
entries = [i.msgid for i in po_file.translated_entries()]
missing = 0
for i in po_ref:
if not i.obsolete:
if not i.msgid in entries:
missing = missing + 1
print(f"::notice file={po}::{flag(lang)} Translation for {repr(i.msgid)} is missing")
if missing > 0:
print(f"::warning file={po}::{flag(lang)} {missing} translations missing")