index.ts 3.7 KB

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