Skip to content

Commit

Permalink
chore(ci): add check for translation keys matching (#5301)
Browse files Browse the repository at this point in the history
  • Loading branch information
MilosPaunovic authored Oct 3, 2024
1 parent 9325668 commit 7b11b1c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
21 changes: 19 additions & 2 deletions .github/workflows/generate_translations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
types: [opened, synchronize]
paths:
- "ui/src/translations/en.json"

push:
branches:
- develop
Expand All @@ -29,12 +30,20 @@ jobs:
with:
python-version: "3.x"

- name: Install dependencies
- name: Install Python dependencies
run: pip install gitpython openai

- name: Generate translations
run: python ui/src/translations/generate_translations.py

- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: "20.x"

- name: Check keys matching
run: node ui/src/translations/check.js

- name: Set up Git
run: |
git config --global user.name "GitHub Action"
Expand Down Expand Up @@ -67,12 +76,20 @@ jobs:
with:
python-version: "3.x"

- name: Install dependencies
- name: Install Python dependencies
run: pip install gitpython openai

- name: Generate translations
run: python ui/src/translations/generate_translations.py

- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: "20.x"

- name: Check keys matching
run: node ui/src/translations/check.js

- name: Set up Git
run: |
git config --global user.name "GitHub Action"
Expand Down
3 changes: 2 additions & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"preview": "vite preview",
"test:unit": "vitest run",
"test:lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix"
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix",
"translations:check": "node ./src/translations/check.js"
},
"dependencies": {
"@js-joda/core": "^5.6.3",
Expand Down
38 changes: 38 additions & 0 deletions ui/src/translations/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import fs from "fs";
import path from "path";
import {fileURLToPath} from "url";

const getPath = (lang) => path.resolve(path.dirname(fileURLToPath(import.meta.url)), `./${lang}.json`);
const readJSON = (filePath) => JSON.parse(fs.readFileSync(filePath, "utf-8"));

const getNestedKeys = (obj, prefix = "") =>
Object.keys(obj).reduce((keys, key) => {
const fullKey = prefix ? `${prefix}.${key}` : key;
keys.push(fullKey);
if (
typeof obj[key] === "object" &&
obj[key] &&
!Array.isArray(obj[key])
) {
keys.push(...getNestedKeys(obj[key], fullKey));
}
return keys;
}, []);

// Use English as a base language
const content = getNestedKeys(readJSON(getPath("en"))["en"]);

const languages = ["de", "es", "fr", "hi", "it", "ja", "ko", "pl", "pt", "ru", "zh_CN"];
const paths = languages.map((lang) => getPath(lang));

languages.forEach((lang, i) => {
const current = getNestedKeys(readJSON(paths[i])[lang]);

const missing = content.filter((key) => !current.includes(key));
const extra = current.filter((key) => !content.includes(key));

console.log(`---\n\x1b[34mComparison with ${lang.toUpperCase()}\x1b[0m \n`);
console.log(missing.length ? `Missing keys: \x1b[31m${missing.join(", ")}\x1b[0m` : "No missing keys.");
console.log(extra.length ? `Extra keys: \x1b[32m${extra.join(", ")}\x1b[0m` : "No extra keys.");
console.log("---\n");
});

0 comments on commit 7b11b1c

Please sign in to comment.