Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: add new workflow to sync i18n files #3215

Merged
merged 3 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
51 changes: 51 additions & 0 deletions scripts/sync_i18n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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):
Exitare marked this conversation as resolved.
Show resolved Hide resolved
# 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
Helias marked this conversation as resolved.
Show resolved Hide resolved
en_file = Path(translations_dir, "en.json")
sync_keys(translations_dir, en_file)