wwf
3 天以前 a430284aa21e3ae1f0d5654e55b2ad2852519cc2
i18n/auto-gen-i18n.js
@@ -30,20 +30,15 @@
      }
      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}`)
        }
      }
    }
@@ -57,16 +52,7 @@
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)
@@ -96,12 +82,7 @@
  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)
      }
    }))
  }))
}