From 869911fcd50c91f28cf781c5d982b718623ff52f Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sun, 10 Aug 2025 14:19:01 +0200 Subject: [PATCH 1/2] pythonize check_messages (merged into check_po) --- .github/workflows/Locales.yml | 2 +- tools/check_messages.sh | 56 --------------- tools/check_po.py | 124 ++++++++++++++++++++++++---------- 3 files changed, 90 insertions(+), 92 deletions(-) delete mode 100755 tools/check_messages.sh diff --git a/.github/workflows/Locales.yml b/.github/workflows/Locales.yml index 1cefef9b..42fa903b 100644 --- a/.github/workflows/Locales.yml +++ b/.github/workflows/Locales.yml @@ -15,4 +15,4 @@ jobs: run: | qtchooser -install qt6 $(which qmake6) export QT_SELECT=qt6 - tools/check_messages.sh + tools/check_po.py diff --git a/tools/check_messages.sh b/tools/check_messages.sh deleted file mode 100755 index 73917740..00000000 --- a/tools/check_messages.sh +++ /dev/null @@ -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 diff --git a/tools/check_po.py b/tools/check_po.py index 91f6f2e4..118243d9 100755 --- a/tools/check_po.py +++ b/tools/check_po.py @@ -3,49 +3,103 @@ import sys import polib import argparse +import os import pathlib +import subprocess +import tempfile -parser = argparse.ArgumentParser( - prog='check_po.py', - description='Check that all strings are translated in a .po file') +def list_files(base: str, exts: str|list[str]): + if type(exts) == str: + 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) -parser.add_argument('file', type=pathlib.Path) -parser.add_argument('lang', type=str) +def flag(lang: str): + # Map language code to a country flag + 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) -entries = [i.msgid for i in po_file.translated_entries()] +if __name__ == "__main__": + 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 -countries = { - 'es': 'es', - 'fr': 'fr', - 'it': 'it', - 'ja': 'jp', - 'zh_TW': 'tw', - 'pt_BR': 'br' -} + parser = argparse.ArgumentParser( + prog='check_po.py', + description='Check that all strings are translated in a .po file') -if args.lang in countries: - country = countries[args.lang] -else: - country = args.lang + parser.add_argument('lang', type=str, nargs="*") -flag = '' -for i in country.upper(): - flag += chr(ord(i) + ord('🇦') - ord('A')) + args = parser.parse_args() -missing = 0 -for i in po_ref: - if not i.obsolete: - if not i.msgid in entries: - missing = missing + 1 - print(f"::notice file={args.file}::{flag} Translation for {repr(i.msgid)} is missing") + if len(args.lang) == 0: + for d in sorted(os.listdir(locale_dir)): + if os.path.isdir(os.path.join(locale_dir, d)): + args.lang.append(d) -if missing > 0: - print(f"::warning file={args.file}::{flag} {missing} translations missing") - sys.exit(1) + with tempfile.TemporaryDirectory() as tmpdir: + client_pot = os.path.join(tmpdir, "client.pot") + 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") From 810c9bd2fa8393b139246a3635b01840e13b0688 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sun, 10 Aug 2025 14:59:50 +0200 Subject: [PATCH 2/2] Manage localisation issues from CI --- .github/workflows/Locales.yml | 12 ++++++++---- tools/check_po.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/.github/workflows/Locales.yml b/.github/workflows/Locales.yml index 42fa903b..722586f6 100644 --- a/.github/workflows/Locales.yml +++ b/.github/workflows/Locales.yml @@ -9,10 +9,14 @@ jobs: - uses: actions/checkout@v4 - name: Install dependencies - run: sudo apt-get update && sudo apt-get install gettext qt6-base-dev qt6-tools-dev qt6-l10n-tools qtchooser python3-polib + run: | + sudo apt-get update && sudo apt-get install gettext + pip install polib PyGithub - name: Check that .po files are up to date run: | - qtchooser -install qt6 $(which qmake6) - export QT_SELECT=qt6 - tools/check_po.py + if [[ "${{github.ref}}" == 'refs/heads/master' ]] ; then + tools/check_po.py --manage-issues --github-repository "${{ github.repository }}" --github-token "${{ github.token }}" + else + tools/check_po.py + fi diff --git a/tools/check_po.py b/tools/check_po.py index 118243d9..c311b751 100755 --- a/tools/check_po.py +++ b/tools/check_po.py @@ -1,6 +1,5 @@ #!/usr/bin/env python -import sys import polib import argparse import os @@ -35,6 +34,9 @@ def flag(lang: str): res += chr(ord(i) + ord('🇦') - ord('A')) return res +ISSUE_LABEL="localisation" +ISSUE_TITLE="[{lang}] Missing translations" + if __name__ == "__main__": root = os.path.dirname(os.path.dirname(__file__)) locale_dir = os.path.join(root, "locale") @@ -45,6 +47,9 @@ if __name__ == "__main__": description='Check that all strings are translated in a .po file') parser.add_argument('lang', type=str, nargs="*") + parser.add_argument('--github-repository') + parser.add_argument('--github-token') + parser.add_argument('--manage-issues', action="store_true") args = parser.parse_args() @@ -53,6 +58,11 @@ if __name__ == "__main__": if os.path.isdir(os.path.join(locale_dir, d)): args.lang.append(d) + if args.manage_issues: + from github import Github, Auth + gh = Github(auth=Auth.Token(args.github_token)) + repo = gh.get_repo(args.github_repository) + with tempfile.TemporaryDirectory() as tmpdir: client_pot = os.path.join(tmpdir, "client.pot") dashboard_pot = os.path.join(tmpdir, "dashboard.pot") @@ -103,3 +113,20 @@ if __name__ == "__main__": if missing > 0: print(f"::warning file={po}::{flag(lang)} {missing} translations missing") + + if args.manage_issues: + issues = [i for i in repo.get_issues(state="all", labels = [ISSUE_LABEL]) if i.title == ISSUE_TITLE.format(lang=lang)] + if not issues: + issue = repo.create_issue( + title=ISSUE_TITLE.format(lang=lang), + labels = [ISSUE_LABEL] + ) + else: + for issue in issues: + if issue.state != "open": + issue.edit(state = "open") + else: + if args.manage_issues: + issues = [i for i in repo.get_issues(state="open", labels = [ISSUE_LABEL]) if i.title == ISSUE_TITLE.format(lang=lang)] + for issue in issues: + issue.edit(state = "closed")