index.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import i18n from 'i18next';
  2. import { initReactI18next } from 'react-i18next';
  3. import LanguageDetector from 'i18next-browser-languagedetector';
  4. // Import translations directly for bundling
  5. import en from './locales/en';
  6. import de from './locales/de';
  7. import es from './locales/es';
  8. import fr from './locales/fr';
  9. import ja from './locales/ja';
  10. import it from './locales/it';
  11. import ko from './locales/ko';
  12. import ptBR from './locales/pt-BR';
  13. import zhCN from './locales/zh-CN';
  14. import zhTW from './locales/zh-TW';
  15. import tr from './locales/tr';
  16. import ru from './locales/ru';
  17. const resources = {
  18. en: { translation: en },
  19. de: { translation: de },
  20. es: { translation: es },
  21. fr: { translation: fr },
  22. ja: { translation: ja },
  23. it: { translation: it },
  24. ko: { translation: ko },
  25. 'pt-BR': { translation: ptBR },
  26. 'zh-CN': { translation: zhCN },
  27. 'zh-TW': { translation: zhTW },
  28. tr: { translation: tr },
  29. ru: { translation: ru },
  30. };
  31. const SUPPORTED_LNGS = ['en', 'de', 'es', 'fr', 'ja', 'it', 'ko', 'pt-BR', 'ru', 'tr', 'zh-CN', 'zh-TW'];
  32. const APPLIANCE_CONSUMED_KEY = 'bambuddy_appliance_locale_consumed';
  33. i18n
  34. .use(LanguageDetector)
  35. .use(initReactI18next)
  36. .init({
  37. resources,
  38. fallbackLng: 'en',
  39. supportedLngs: SUPPORTED_LNGS,
  40. detection: {
  41. // Order of detection methods
  42. order: ['localStorage', 'navigator', 'htmlTag'],
  43. // Key to use in localStorage
  44. lookupLocalStorage: 'bambutrack_language',
  45. // Cache user language
  46. caches: ['localStorage'],
  47. },
  48. interpolation: {
  49. escapeValue: false, // React already escapes
  50. },
  51. react: {
  52. useSuspense: false,
  53. },
  54. });
  55. /**
  56. * Bambuddy Appliance hook: on the first SPA load after the firstboot wizard
  57. * runs, /api/v1/system/appliance returns the locale the user picked. We
  58. * apply it once (gated by a localStorage flag) and stop. On non-appliance
  59. * installs the endpoint either 404s or returns nulls — silent no-op.
  60. *
  61. * This runs AFTER i18n.init so the LanguageDetector has already populated a
  62. * default; we override that default exactly once for fresh appliances. The
  63. * appliance is then "consumed" and the language picker is the only way to
  64. * change locale going forward (the wizard ran once; future intent comes from
  65. * the running UI).
  66. */
  67. function applyApplianceLocale() {
  68. if (typeof window === 'undefined' || !window.localStorage) return;
  69. const storage = window.localStorage;
  70. if (typeof storage.getItem !== 'function' || typeof storage.setItem !== 'function') return;
  71. if (storage.getItem(APPLIANCE_CONSUMED_KEY)) return;
  72. fetch('/api/v1/system/appliance')
  73. .then((r) => (r.ok ? r.json() : null))
  74. .then((data) => {
  75. if (!data || typeof data.locale !== 'string') return;
  76. if (!SUPPORTED_LNGS.includes(data.locale)) return;
  77. i18n.changeLanguage(data.locale);
  78. storage.setItem(APPLIANCE_CONSUMED_KEY, '1');
  79. })
  80. .catch(() => {
  81. // Endpoint absent or unreachable — non-appliance install or dev environment.
  82. // Leave the detector's choice in place.
  83. });
  84. }
  85. applyApplianceLocale();
  86. export default i18n;
  87. // Helper to get available languages
  88. export const availableLanguages = [
  89. { code: 'en', name: 'English', nativeName: 'English' },
  90. { code: 'de', name: 'German', nativeName: 'Deutsch' },
  91. { code: 'es', name: 'Spanish', nativeName: 'Español' },
  92. { code: 'fr', name: 'French', nativeName: 'Français' },
  93. { code: 'ja', name: 'Japanese', nativeName: '日本語' },
  94. { code: 'it', name: 'Italian', nativeName: 'Italiano' },
  95. { code: 'ko', name: 'Korean', nativeName: '한국어' },
  96. { code: 'pt-BR', name: 'Portuguese (Brazil)', nativeName: 'Português (Brasil)' },
  97. { code: 'zh-CN', name: 'Chinese (Simplified)', nativeName: '简体中文' },
  98. { code: 'zh-TW', name: 'Chinese (Traditional)', nativeName: '繁體中文' },
  99. { code: 'tr', name: 'Turkish', nativeName: 'Türkçe' },
  100. { code: 'ru', name: 'Russian', nativeName: 'Русский' },
  101. ];