From b40853af941e7255e26d4c5a0eb720b38b518b26 Mon Sep 17 00:00:00 2001 From: ElectricalBoy <15651807+ElectricalBoy@users.noreply.github.com> Date: Mon, 29 Jul 2024 21:38:10 +0900 Subject: [PATCH] CI: Create Automated Workflow for Translation Catalog Updates (#5959) * CI: Automate translation catalog update Signed-off-by: GitHub * Update triggers for localization workflow Signed-off-by: GitHub * Rewrite HintsToPot with Python * Disable scheduled workflow runs * Apply formatting to HintsToPot.py * Add missing exit() call --- .github/workflows/update-translation.yml | 38 ++++++++++++++++++++++++ run_gettext.bat | 2 +- run_gettext.sh | 2 +- scripts/HintsToPot.py | 37 +++++++++++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/update-translation.yml create mode 100644 scripts/HintsToPot.py diff --git a/.github/workflows/update-translation.yml b/.github/workflows/update-translation.yml new file mode 100644 index 000000000..af97e606e --- /dev/null +++ b/.github/workflows/update-translation.yml @@ -0,0 +1,38 @@ +name: Update Translation Catalog +on: + # schedule: + # - cron: 0 0 * * 1 + workflow_dispatch: + +jobs: + update_translation: + name: Update translation + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Install gettext + run: | + sudo apt-get update + sudo apt-get install -y gettext + + - name: Update translation catalog + run: | + ./run_gettext.sh --full + git add localization/i18n/* + + - name: Commit translation catalog updates + uses: qoomon/actions--create-commit@v1 + id: commit + with: + message: Update translation catalog + skip-empty: true + + - name: Push changes + run: git push diff --git a/run_gettext.bat b/run_gettext.bat index 9a0425df1..c095a3db0 100644 --- a/run_gettext.bat +++ b/run_gettext.bat @@ -10,7 +10,7 @@ for %%a in (%*) do ( if %FULL_MODE%==1 ( .\tools\xgettext.exe --keyword=L --keyword=_L --keyword=_u8L --keyword=L_CONTEXT:1,2c --keyword=_L_PLURAL:1,2 --add-comments=TRN --from-code=UTF-8 --no-location --debug --boost -f ./localization/i18n/list.txt -o ./localization/i18n/OrcaSlicer.pot - build\\src\\hints\\Release\\hintsToPot ./resources ./localization/i18n + python3 scripts/HintsToPot.py ./resources ./localization/i18n ) REM Print the current directory echo %cd% diff --git a/run_gettext.sh b/run_gettext.sh index f64f50f05..561d6da5e 100755 --- a/run_gettext.sh +++ b/run_gettext.sh @@ -15,7 +15,7 @@ done if $FULL_MODE; then xgettext --keyword=L --keyword=_L --keyword=_u8L --keyword=L_CONTEXT:1,2c --keyword=_L_PLURAL:1,2 --add-comments=TRN --from-code=UTF-8 --no-location --debug --boost -f ./localization/i18n/list.txt -o ./localization/i18n/OrcaSlicer.pot - ./build_arm64/src/hints/Release/hintsToPot.app/Contents/MacOS/hintsToPot ./resources ./localization/i18n + python3 scripts/HintsToPot.py ./resources ./localization/i18n fi diff --git a/scripts/HintsToPot.py b/scripts/HintsToPot.py new file mode 100644 index 000000000..9fef62ee8 --- /dev/null +++ b/scripts/HintsToPot.py @@ -0,0 +1,37 @@ +# Helps converting hints.ini into POT + +import sys + +from configparser import ConfigParser +from pathlib import Path + + +def write_to_pot(path: Path, data: dict[str, str]): + with open(path, "a+t") as pot_file: + for key in data.keys(): + print( + f"\n#: resources/data/hints.ini: [{ key }]\nmsgid \"{ data[key]['text'] }\"\nmsgstr \"\"", + file=pot_file, + ) + + +def main(): + if len(sys.argv) != 3: + print("HINTS_TO_POT FAILED: WRONG NUM OF ARGS") + exit(-1) + path_to_ini = Path(sys.argv[1]).parent / "resources" / "data" / "hints.ini" + path_to_pot = Path(sys.argv[2]).parent / "i18n" / "OrcaSlicer.pot" + if not path_to_ini.exists(): + print("HINTS_TO_POT FAILED: PATH TO INI DOES NOT EXISTS") + print(str(path_to_ini)) + exit(-1) + config = ConfigParser() + with open(path_to_ini) as hints_file: + config.read_file(hints_file) + write_to_pot(path_to_pot, config._sections) + print("HINTS_TO_POT SUCCESS") + exit(0) + + +if __name__ == "__main__": + main()