auto-gen-i18n.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* eslint-disable no-eval */
  2. const fs = require('node:fs')
  3. const path = require('node:path')
  4. const transpile = require('typescript').transpile
  5. const magicast = require('magicast')
  6. const { parseModule, generateCode, loadFile } = magicast
  7. const bingTranslate = require('bing-translate-api')
  8. const { translate } = bingTranslate
  9. const data = require('./languages.json')
  10. const targetLanguage = 'en-US'
  11. // https://github.com/plainheart/bing-translate-api/blob/master/src/met/lang.json
  12. const languageKeyMap = data.languages.reduce((map, language) => {
  13. if (language.supported) {
  14. if (language.value === 'zh-Hans' || language.value === 'zh-Hant')
  15. map[language.value] = language.value
  16. else
  17. map[language.value] = language.value.split('-')[0]
  18. }
  19. return map
  20. }, {})
  21. async function translateMissingKeyDeeply(sourceObj, targetObject, toLanguage) {
  22. await Promise.all(Object.keys(sourceObj).map(async (key) => {
  23. if (targetObject[key] === undefined) {
  24. if (typeof sourceObj[key] === 'object') {
  25. targetObject[key] = {}
  26. await translateMissingKeyDeeply(sourceObj[key], targetObject[key], toLanguage)
  27. }
  28. else {
  29. const { translation } = await translate(sourceObj[key], null, languageKeyMap[toLanguage])
  30. targetObject[key] = translation
  31. // console.log(translation)
  32. }
  33. }
  34. else if (typeof sourceObj[key] === 'object') {
  35. targetObject[key] = targetObject[key] || {}
  36. await translateMissingKeyDeeply(sourceObj[key], targetObject[key], toLanguage)
  37. }
  38. }))
  39. }
  40. async function autoGenTrans(fileName, toGenLanguage) {
  41. const fullKeyFilePath = path.join(__dirname, targetLanguage, `${fileName}.ts`)
  42. const toGenLanguageFilePath = path.join(__dirname, toGenLanguage, `${fileName}.ts`)
  43. const fullKeyContent = eval(transpile(fs.readFileSync(fullKeyFilePath, 'utf8')))
  44. // To keep object format and format it for magicast to work: const translation = { ... } => export default {...}
  45. const readContent = await loadFile(toGenLanguageFilePath)
  46. const { code: toGenContent } = generateCode(readContent)
  47. const mod = await parseModule(`export default ${toGenContent.replace('export default translation', '').replace('const translation = ', '')}`)
  48. const toGenOutPut = mod.exports.default
  49. await translateMissingKeyDeeply(fullKeyContent, toGenOutPut, toGenLanguage)
  50. const { code } = generateCode(mod)
  51. const res = `const translation =${code.replace('export default', '')}
  52. export default translation
  53. `.replace(/,\n\n/g, ',\n').replace('};', '}')
  54. fs.writeFileSync(toGenLanguageFilePath, res)
  55. }
  56. async function main() {
  57. // const fileName = 'workflow'
  58. // Promise.all(Object.keys(languageKeyMap).map(async (toLanguage) => {
  59. // await autoGenTrans(fileName, toLanguage)
  60. // }))
  61. const files = fs
  62. .readdirSync(path.join(__dirname, targetLanguage))
  63. .map(file => file.replace(/\.ts/, ''))
  64. .filter(f => f !== 'app-debug') // ast parse error in app-debug
  65. await Promise.all(files.map(async (file) => {
  66. await Promise.all(Object.keys(languageKeyMap).map(async (language) => {
  67. await autoGenTrans(file, language)
  68. }))
  69. }))
  70. }
  71. main()