transformLocales.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import Path from 'path'
  2. import fs from 'fs'
  3. import { fileURLToPath } from 'node:url'
  4. import { loadLocale } from './utils.js'
  5. const __dirname = fileURLToPath(new URL('.', import.meta.url))
  6. const LANGUAGES = [
  7. 'cs',
  8. 'da',
  9. 'de',
  10. 'en',
  11. 'es',
  12. 'fi',
  13. 'fr',
  14. 'it',
  15. 'ja',
  16. 'ko',
  17. 'nl',
  18. 'no',
  19. 'pl',
  20. 'pt',
  21. 'ru',
  22. 'sv',
  23. 'tr',
  24. 'zh-CN',
  25. ]
  26. const LOCALES = {}
  27. LANGUAGES.forEach(loadLocales)
  28. function loadLocales(language) {
  29. LOCALES[language] = loadLocale(language)
  30. }
  31. function transformLocales(mapping, transformLocale) {
  32. Object.entries(LOCALES).forEach(([language, translatedLocales]) => {
  33. Object.entries(mapping).forEach(([localeKey, spec]) => {
  34. const locale = translatedLocales[localeKey]
  35. if (!locale) {
  36. // This locale is not translated yet.
  37. return
  38. }
  39. translatedLocales[localeKey] = transformLocale(locale, spec)
  40. })
  41. fs.writeFileSync(
  42. Path.join(__dirname, `/../../locales/${language}.json`),
  43. JSON.stringify(translatedLocales, null, 2) + '\n'
  44. )
  45. })
  46. }
  47. export default {
  48. transformLocales,
  49. }