-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: add new workflow to sync i18n files (#3215)
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
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,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 |
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,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) |