-
-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add workflow to check for translation issues
- Loading branch information
1 parent
3a06d01
commit 6666d08
Showing
4 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Check Translations | ||
|
||
on: | ||
push: | ||
branches-ignore: | ||
- "dependabot/**" | ||
tags-ignore: | ||
- "**" | ||
paths: | ||
- "src/main/resources/assets/wurst/translations/**.json" | ||
pull_request: | ||
paths: | ||
- "src/main/resources/assets/wurst/translations/**.json" | ||
workflow_dispatch: | ||
|
||
jobs: | ||
check_translations: | ||
runs-on: ubuntu-latest | ||
steps: | ||
|
||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Python 3.12 | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: "3.12" | ||
|
||
- name: Run check_translations.py | ||
run: python scripts/check_translations.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import util | ||
from pathlib import Path | ||
|
||
translations_dir = Path("src") / "main" / "resources" / "assets" / "wurst" / "translations" | ||
|
||
|
||
def show_translation_stats(en_us: dict, translations: dict): | ||
"""Render a table of the current translation progress for each language.""" | ||
util.add_github_summary("| Language | Translated | % |") | ||
util.add_github_summary("| --- | --- | --- |") | ||
util.add_github_summary(f"| en_us | {len(en_us)} | 100.00% |") | ||
for lang, data in translations.items(): | ||
util.add_github_summary(f"| {lang} | {len(data)} | {len(data) / len(en_us) * 100:.2f}% |") | ||
util.add_github_summary("") | ||
|
||
|
||
def check_extra_keys(en_us: dict, translations: dict): | ||
"""Check if any translation files contain keys that don't exist in the original.""" | ||
extra_keys_found = False | ||
for lang, data in translations.items(): | ||
extra_keys = set(data.keys()) - set(en_us.keys()) | ||
if extra_keys: | ||
extra_keys_found = True | ||
util.add_github_summary( | ||
f"⚠ {lang}.json contains translations that don't exist in en_us.json ({len(extra_keys)} found):" | ||
) | ||
for key in extra_keys: | ||
util.add_github_summary(f"- {key}") | ||
if extra_keys_found: | ||
raise Exception("Found extra keys in one or more translation files, see summary") | ||
|
||
|
||
def main(): | ||
en_us = util.read_json_file(translations_dir / "en_us.json") | ||
translations = {} | ||
for path in translations_dir.rglob("*.json"): | ||
if path.is_file() and path.name != "en_us.json": | ||
lang = path.name.removesuffix(".json") | ||
data = util.read_json_file(path) | ||
translations[lang] = data | ||
|
||
show_translation_stats(en_us, translations) | ||
check_extra_keys(en_us, translations) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import json | ||
import os | ||
from pathlib import Path | ||
|
||
|
||
def read_json_file(path: Path) -> dict: | ||
"""Read a JSON data file.""" | ||
return json.loads(path.read_text(encoding="utf-8")) | ||
|
||
|
||
def add_github_summary(summary: str): | ||
"""Add a line to the GitHub Actions summary for the current step.""" | ||
if "GITHUB_STEP_SUMMARY" not in os.environ: | ||
print(f"Not running on GHA, would have set summary: {summary}") | ||
return | ||
with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as summary_file: | ||
print(summary, file=summary_file) |