check-i18n.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 targetLanguage = 'en-US'
  6. const data = require('./languages.json')
  7. const languages = data.languages.filter(language => language.supported).map(language => language.value)
  8. async function getKeysFromLanuage(language) {
  9. return new Promise((resolve, reject) => {
  10. const folderPath = path.join(__dirname, language)
  11. let allKeys = []
  12. fs.readdir(folderPath, (err, files) => {
  13. if (err) {
  14. console.error('Error reading folder:', err)
  15. reject(err)
  16. return
  17. }
  18. files.forEach((file) => {
  19. const filePath = path.join(folderPath, file)
  20. const fileName = file.replace(/\.[^/.]+$/, '') // Remove file extension
  21. const camelCaseFileName = fileName.replace(/[-_](.)/g, (_, c) =>
  22. c.toUpperCase(),
  23. ) // Convert to camel case
  24. // console.log(camelCaseFileName)
  25. const content = fs.readFileSync(filePath, 'utf8')
  26. const translation = eval(transpile(content))
  27. // console.log(translation)
  28. const keys = Object.keys(translation)
  29. const nestedKeys = []
  30. const iterateKeys = (obj, prefix = '') => {
  31. for (const key in obj) {
  32. const nestedKey = prefix ? `${prefix}.${key}` : key
  33. nestedKeys.push(nestedKey)
  34. if (typeof obj[key] === 'object')
  35. iterateKeys(obj[key], nestedKey)
  36. }
  37. }
  38. iterateKeys(translation)
  39. allKeys = [...keys, ...nestedKeys].map(
  40. key => `${camelCaseFileName}.${key}`,
  41. )
  42. })
  43. resolve(allKeys)
  44. })
  45. })
  46. }
  47. async function main() {
  48. const compareKeysCount = async () => {
  49. const targetKeys = await getKeysFromLanuage(targetLanguage)
  50. const languagesKeys = await Promise.all(languages.map(language => getKeysFromLanuage(language)))
  51. const keysCount = languagesKeys.map(keys => keys.length)
  52. const targetKeysCount = targetKeys.length
  53. const comparison = languages.reduce((result, language, index) => {
  54. const languageKeysCount = keysCount[index]
  55. const difference = targetKeysCount - languageKeysCount
  56. result[language] = difference
  57. return result
  58. }, {})
  59. console.log(comparison)
  60. // Print missing keys
  61. languages.forEach((language, index) => {
  62. const missingKeys = targetKeys.filter(key => !languagesKeys[index].includes(key))
  63. console.log(`Missing keys in ${language}:`, missingKeys)
  64. })
  65. }
  66. compareKeysCount()
  67. }
  68. main()