| | |
| | | } |
| | | else { |
| | | try { |
| | | const source = sourceObj[key] |
| | | if (!source) { |
| | | if (!sourceObj[key]) { |
| | | targetObject[key] = '' |
| | | return |
| | | } |
| | | // not support translate with '(' or ')' |
| | | if (source.includes('(') || source.includes(')')) |
| | | return |
| | | |
| | | const { translation } = await translate(sourceObj[key], null, languageKeyMap[toLanguage]) |
| | | targetObject[key] = translation |
| | | } |
| | | catch { |
| | | console.error(`Error translating "${sourceObj[key]}" to ${toLanguage}. Key: ${key}`) |
| | | catch (e) { |
| | | console.error(`Error translating ${sourceObj[key]}(${key}) to ${toLanguage}`) |
| | | } |
| | | } |
| | | } |
| | |
| | | async function autoGenTrans(fileName, toGenLanguage) { |
| | | const fullKeyFilePath = path.join(__dirname, targetLanguage, `${fileName}.ts`) |
| | | const toGenLanguageFilePath = path.join(__dirname, toGenLanguage, `${fileName}.ts`) |
| | | // eslint-disable-next-line sonarjs/code-eval |
| | | const fullKeyContent = eval(transpile(fs.readFileSync(fullKeyFilePath, 'utf8'))) |
| | | // if toGenLanguageFilePath is not exist, create it |
| | | if (!fs.existsSync(toGenLanguageFilePath)) { |
| | | fs.writeFileSync(toGenLanguageFilePath, `const translation = { |
| | | } |
| | | |
| | | export default translation |
| | | `) |
| | | } |
| | | // To keep object format and format it for magicast to work: const translation = { ... } => export default {...} |
| | | const readContent = await loadFile(toGenLanguageFilePath) |
| | | const { code: toGenContent } = generateCode(readContent) |
| | |
| | | |
| | | await Promise.all(files.map(async (file) => { |
| | | await Promise.all(Object.keys(languageKeyMap).map(async (language) => { |
| | | try { |
| | | await autoGenTrans(file, language) |
| | | } |
| | | catch (e) { |
| | | console.error(`Error translating ${file} to ${language}`, e) |
| | | } |
| | | await autoGenTrans(file, language) |
| | | })) |
| | | })) |
| | | } |