genenrate-i18n.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import fs from 'node:fs';
  2. /**
  3. * @url https://developer.chrome.com/docs/extensions/reference/api/i18n#support_multiple_languages
  4. */
  5. const SUPPORTED_LANGUAGES = {
  6. ar: 'Arabic',
  7. am: 'Amharic',
  8. bg: 'Bulgarian',
  9. bn: 'Bengali',
  10. ca: 'Catalan',
  11. cs: 'Czech',
  12. da: 'Danish',
  13. de: 'German',
  14. el: 'Greek',
  15. en: 'English',
  16. en_AU: 'English (Australia)',
  17. en_GB: 'English (Great Britain)',
  18. en_US: 'English (USA)',
  19. es: 'Spanish',
  20. es_419: 'Spanish (Latin America and Caribbean)',
  21. et: 'Estonian',
  22. fa: 'Persian',
  23. fi: 'Finnish',
  24. fil: 'Filipino',
  25. fr: 'French',
  26. gu: 'Gujarati',
  27. he: 'Hebrew',
  28. hi: 'Hindi',
  29. hr: 'Croatian',
  30. hu: 'Hungarian',
  31. id: 'Indonesian',
  32. it: 'Italian',
  33. ja: 'Japanese',
  34. kn: 'Kannada',
  35. ko: 'Korean',
  36. lt: 'Lithuanian',
  37. lv: 'Latvian',
  38. ml: 'Malayalam',
  39. mr: 'Marathi',
  40. ms: 'Malay',
  41. nl: 'Dutch',
  42. no: 'Norwegian',
  43. pl: 'Polish',
  44. pt_BR: 'Portuguese (Brazil)',
  45. pt_PT: 'Portuguese (Portugal)',
  46. ro: 'Romanian',
  47. ru: 'Russian',
  48. sk: 'Slovak',
  49. sl: 'Slovenian',
  50. sr: 'Serbian',
  51. sv: 'Swedish',
  52. sw: 'Swahili',
  53. ta: 'Tamil',
  54. te: 'Telugu',
  55. th: 'Thai',
  56. tr: 'Turkish',
  57. uk: 'Ukrainian',
  58. vi: 'Vietnamese',
  59. zh_CN: 'Chinese (China)',
  60. zh_TW: 'Chinese (Taiwan)',
  61. };
  62. const locales = fs.readdirSync('locales');
  63. locales.forEach(locale => {
  64. if (!(locale in SUPPORTED_LANGUAGES)) {
  65. throw new Error(`Unsupported language: ${locale}`);
  66. }
  67. });
  68. makeTypeFile(locales);
  69. makeGetMessageFromLocaleFile(locales);
  70. function makeTypeFile(locales) {
  71. const typeFile = `/**
  72. * This file is generated by generate-i18n.mjs
  73. * Do not edit this file directly
  74. */
  75. ${locales.map(locale => `import type ${locale}Message from '../locales/${locale}/messages.json';`).join('\n')}
  76. export type MessageKey = ${locales.map(locale => `keyof typeof ${locale}Message`).join(' & ')};
  77. export type DevLocale = ${locales.map(locale => `'${locale}'`).join(' | ')};
  78. `;
  79. fs.writeFileSync('lib/type.ts', typeFile);
  80. }
  81. function makeGetMessageFromLocaleFile(locales) {
  82. const defaultLocaleCode = `(() => {
  83. const locales = ${JSON.stringify(locales).replace(/"/g, "'").replace(/,/g, ', ')};
  84. const firstLocale = locales[0];
  85. const defaultLocale = Intl.DateTimeFormat().resolvedOptions().locale.replace('-', '_');
  86. if (locales.includes(defaultLocale)) {
  87. return defaultLocale;
  88. }
  89. const defaultLocaleWithoutRegion = defaultLocale.split('_')[0];
  90. if (locales.includes(defaultLocaleWithoutRegion)) {
  91. return defaultLocaleWithoutRegion;
  92. }
  93. return firstLocale;
  94. })()`;
  95. const getMessageFromLocaleFile = `/**
  96. * This file is generated by generate-i18n.mjs
  97. * Do not edit this file directly
  98. */
  99. ${locales.map(locale => `import ${locale}Message from '../locales/${locale}/messages.json';`).join('\n')}
  100. export function getMessageFromLocale(locale: string) {
  101. switch (locale) {
  102. ${locales
  103. .map(
  104. locale => ` case '${locale}':
  105. return ${locale}Message;`,
  106. )
  107. .join('\n')}
  108. default:
  109. throw new Error('Unsupported locale');
  110. }
  111. }
  112. export const defaultLocale = ${defaultLocaleCode};
  113. `;
  114. fs.writeFileSync('lib/getMessageFromLocale.ts', getMessageFromLocaleFile);
  115. }