mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-05-17 16:46:55 +08:00
Feat/add script to check i18n keys (#2835)
This commit is contained in:
parent
4d63770189
commit
af98954fc1
77
web/i18n/script.js
Normal file
77
web/i18n/script.js
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
/* eslint-disable no-eval */
|
||||||
|
const fs = require('node:fs')
|
||||||
|
const path = require('node:path')
|
||||||
|
const transpile = require('typescript').transpile
|
||||||
|
|
||||||
|
const targetLanguage = 'en-US'
|
||||||
|
const languages = ['zh-Hans', 'fr-FR', 'ja-JP', 'pt-BR', 'uk-UA', 'vi-VN']
|
||||||
|
|
||||||
|
async function getKeysFromLanuage(language) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const folderPath = path.join(__dirname, language)
|
||||||
|
let allKeys = []
|
||||||
|
fs.readdir(folderPath, (err, files) => {
|
||||||
|
if (err) {
|
||||||
|
console.error('Error reading folder:', err)
|
||||||
|
reject(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
files.forEach((file) => {
|
||||||
|
const filePath = path.join(folderPath, file)
|
||||||
|
const fileName = file.replace(/\.[^/.]+$/, '') // Remove file extension
|
||||||
|
const camelCaseFileName = fileName.replace(/[-_](.)/g, (_, c) =>
|
||||||
|
c.toUpperCase(),
|
||||||
|
) // Convert to camel case
|
||||||
|
// console.log(camelCaseFileName)
|
||||||
|
const content = fs.readFileSync(filePath, 'utf8')
|
||||||
|
const translation = eval(transpile(content))
|
||||||
|
const keys = Object.keys(translation)
|
||||||
|
const nestedKeys = []
|
||||||
|
const iterateKeys = (obj, prefix = '') => {
|
||||||
|
for (const key in obj) {
|
||||||
|
const nestedKey = prefix ? `${prefix}.${key}` : key
|
||||||
|
nestedKeys.push(nestedKey)
|
||||||
|
if (typeof obj[key] === 'object')
|
||||||
|
iterateKeys(obj[key], nestedKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
iterateKeys(translation)
|
||||||
|
|
||||||
|
allKeys = [...keys, ...nestedKeys].map(
|
||||||
|
key => `${camelCaseFileName}.${key}`,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
resolve(allKeys)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
const compareKeysCount = async () => {
|
||||||
|
const targetKeys = await getKeysFromLanuage(targetLanguage)
|
||||||
|
const languagesKeys = await Promise.all(languages.map(language => getKeysFromLanuage(language)))
|
||||||
|
|
||||||
|
const keysCount = languagesKeys.map(keys => keys.length)
|
||||||
|
const targetKeysCount = targetKeys.length
|
||||||
|
|
||||||
|
const comparison = languages.reduce((result, language, index) => {
|
||||||
|
const languageKeysCount = keysCount[index]
|
||||||
|
const difference = targetKeysCount - languageKeysCount
|
||||||
|
result[language] = difference
|
||||||
|
return result
|
||||||
|
}, {})
|
||||||
|
|
||||||
|
console.log(comparison)
|
||||||
|
|
||||||
|
// Print missing keys
|
||||||
|
languages.forEach((language, index) => {
|
||||||
|
const missingKeys = targetKeys.filter(key => !languagesKeys[index].includes(key))
|
||||||
|
console.log(`Missing keys in ${language}:`, missingKeys)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
compareKeysCount()
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
@ -11,7 +11,8 @@
|
|||||||
"eslint-fix": "eslint --fix",
|
"eslint-fix": "eslint --fix",
|
||||||
"prepare": "cd ../ && node -e \"if (process.env.NODE_ENV !== 'production'){process.exit(1)} \" || husky install ./web/.husky",
|
"prepare": "cd ../ && node -e \"if (process.env.NODE_ENV !== 'production'){process.exit(1)} \" || husky install ./web/.husky",
|
||||||
"gen-icons": "node ./app/components/base/icons/script.js",
|
"gen-icons": "node ./app/components/base/icons/script.js",
|
||||||
"uglify-embed": "node ./bin/uglify-embed"
|
"uglify-embed": "node ./bin/uglify-embed",
|
||||||
|
"check-i18n": "node ./i18n/script.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/runtime": "^7.22.3",
|
"@babel/runtime": "^7.22.3",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user