agama/.github/workflows/weblate-merge-po.yml
Ladislav Slezák f83484bd61
Refactoring the web translations (#1777)
## Problem

- The web translations still use the original Cockpit mechanism for
creating and loading the translations
- That uses a server based logic which has quite some drawbacks
- We have to reimplement the very same logic in a Webpack plugin to make
it work also in the local development server (`npm run server`). And
unfortunately we cannot share the code because it is Rust vs.
Javascript...
- This will not work in the Agama online demo site with completely
static files (without any server side logic, everything needs to be on
the client side).
- For me the original logic was quite strange, why the server should
decide which translations should the client use? It should be completely
handled on the client side, there is no reason for the special server
logic. The server should just report the available translations and let
the client to pick and use the right one.
- The old implementation has also some ugly parts like requiring a
global Javascript variable...

## Solution

- Move the translation logic from the server to the client (browser)
- Use dynamic imports instead of HTTP redirection

## Implementation details

- Removed the `po.js` handler from the Agama HTTP server
- Removed the `po.js` handler from the Webpack development server
- Removed the original Gettext PO files from Git, replaced by already
converted JS files
- Removed the Cockpit Webpack plugin for converting PO files to JS,
replaced by a simple script with similar functionality. (As a bonus the
code was fixed to avoid adding unnecessary `null` values into the
output.)
- Added dynamic import for downloading the requested translations
- Some code cleanup (removing the global JS object)
- Moved the `jed` and `gettext-parser` NPM dependencies to the PO->JS
converter, that means those NPM packages are installed only when
converting the PO files to JS (basically only in the GitHub action which
merges them), they are not installed when building the Agama web
frontend

## Testing

- Tested manually, loading the translations works fine 
- I manually triggered the GitHub Action which merges the translations
from Weblate. It works fine, the test run opened this [pull
request](https://github.com/agama-project/agama/pull/1780) with updated
translations (there are more changes as it was executed in the same
branch as this pull request).
2024-11-28 17:13:19 +01:00

119 lines
4 KiB
YAML

name: Weblate Merge PO
on:
schedule:
# run every Monday at 2:42AM UTC
- cron: "42 2 * * 0"
# allow running manually
workflow_dispatch:
jobs:
merge-po:
# allow pushing and creating pull requests
permissions:
contents: write
pull-requests: write
# do not run in forks
if: ${{ !github.event.repository.fork }}
runs-on: ubuntu-latest
container:
image: registry.opensuse.org/opensuse/tumbleweed:latest
volumes:
# bind mount the GitHub CLI tool from the Ubuntu host,
# it is a statically linked binary so it should work also in TumbleWeed
- /usr/bin/gh:/usr/bin/gh
steps:
- name: Configure and refresh repositories
run: |
# disable unused repositories to have a faster refresh
zypper modifyrepo -d repo-non-oss repo-openh264 repo-update && \
zypper --non-interactive ref
- name: Install tools
run: zypper --non-interactive install --no-recommends nodejs npm git gettext-tools python3-langtable
- name: Configure Git
run: |
git config --global user.name "YaST Bot"
git config --global user.email "yast-devel@opensuse.org"
- name: Checkout sources
uses: actions/checkout@v4
with:
path: agama
- name: Checkout Agama-weblate sources
uses: actions/checkout@v4
with:
path: agama-weblate
repository: ${{ github.repository_owner }}/agama-weblate
- name: Install NPM packages
working-directory: ./agama/web/share/po
run: npm ci
- name: Validate the PO files
working-directory: ./agama-weblate
run: ls web/*.po | xargs -n1 msgfmt --check-format -o /dev/null
- name: Update JS translations
working-directory: ./agama
run: |
mkdir -p web/src/po
# delete the current translations
find web/src/po -name '*.js' -exec git rm '{}' ';'
# update the list of supported languages, it is used by the PO->JS converter in the next step
web/share/update-languages.py --po-directory ../agama-weblate/web > web/src/languages.json
# rebuild the JS files
(cd web/src/po && SRCDIR=../../../../agama-weblate/web ../../share/po/po-converter.js)
# stage the changes
git add web/src/po/*.js web/src/languages.json
# any changes detected?
- name: Check changes
id: check_changes
working-directory: ./agama
run: |
git diff --staged web | tee tr.diff
if [ -s tr.diff ]; then
echo "Translations updated"
# this is an Output Parameter
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter
echo "updated=true" >> $GITHUB_OUTPUT
else
echo "Translations not changed"
echo "updated=false" >> $GITHUB_OUTPUT
fi
rm tr.diff
- name: Push updated PO files
# run only when a PO file has been updated
if: steps.check_changes.outputs.updated == 'true'
working-directory: ./agama
run: |
# use an unique branch to avoid possible conflicts with already existing branches
git checkout -b "po_merge_${GITHUB_RUN_ID}"
git commit -a -m "Update web translation files"$'\n\n'"Agama-weblate commit: `git -C ../agama-weblate rev-parse HEAD`"
git push origin "po_merge_${GITHUB_RUN_ID}"
- name: Create pull request
# run only when a PO file has been updated
if: steps.check_changes.outputs.updated == 'true'
working-directory: ./agama
run: |
gh pr create -B master -H "po_merge_${GITHUB_RUN_ID}" \
--label translations --label bot \
--title "Update web translation files" \
--body "Updating the web translation files from the agama-weblate repository"
env:
GH_TOKEN: ${{ github.token }}