-
Notifications
You must be signed in to change notification settings - Fork 948
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ci): add check for translation keys matching (#5301)
- Loading branch information
1 parent
9325668
commit 7b11b1c
Showing
3 changed files
with
59 additions
and
3 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
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,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"); | ||
}); |