Skip to content

Commit

Permalink
ci: add new workflow to sync i18n files (#3215)
Browse files Browse the repository at this point in the history
  • Loading branch information
Exitare authored Dec 27, 2024
1 parent a1a8269 commit 4442cc8
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/sync_i18n.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Check and Fix Missing Translation Keys

on:
push:
branches:
- '**'
pull_request:
branches:
- main

jobs:
check-translation-keys:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'

- name: Install dependencies
run: pip install json-delta

- name: Run Python script to check and fix missing keys
run: |
python scripts/sync_i18n.py
- name: Commit and push changes if necessary
run: |
if [ "$(git status --porcelain)" != "" ]; then
git config user.name "i18n Sync Bot"
git config user.email "[email protected]"
git add apps/keira/src/assets/i18n/*.json
git commit -m "Fix missing translation keys"
git push
else
echo "No missing keys detected."
fi
49 changes: 49 additions & 0 deletions scripts/sync_i18n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os
import json
from pathlib import Path

def merge_with_en(en_data: dict, target_data: dict) -> dict:
"""
Recursively merge en.json keys into target translation file.
- Adds missing keys from en.json.
- Preserves existing keys and values in the target file.
- Removes keys not present in en.json.
"""
merged = {}
for key, value in en_data.items():
if key in target_data:
# If key exists in both, check if it's a nested dictionary
if isinstance(value, dict) and isinstance(target_data[key], dict):
merged[key] = merge_with_en(value, target_data[key])
else:
merged[key] = target_data[key] # Preserve existing value
else:
merged[key] = value # Add missing key with value from en.json

return merged

def sync_keys(translations_dir: Path, en_file: Path) -> None:
# Load en.json as the reference file
with open(en_file, 'r', encoding='utf-8') as f:
en_data = json.load(f)

for file in os.listdir(translations_dir):
if file.endswith('.json') and file != os.path.basename(en_file):
file_path = os.path.join(translations_dir, file)
with open(file_path, 'r', encoding='utf-8') as f:
target_data = json.load(f)

# Synchronize keys with en.json
synced_data = merge_with_en(en_data, target_data)

# Save the synchronized file
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(synced_data, f, ensure_ascii=False, indent=2)

print(f"Synced {file}")


if __name__ == "__main__":
translations_dir = Path("apps", "keira", "src", "assets", "i18n") # Update to your directory
en_file = Path(translations_dir, "en.json")
sync_keys(translations_dir, en_file)

0 comments on commit 4442cc8

Please sign in to comment.