index.ts 4.0 KB

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