All files / src i18n.ts

100% Statements 6/6
75% Branches 3/4
100% Functions 0/0
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76                                                                                          27x         27x                                 27x 27x 27x     27x      
/**
 * @file i18n.ts
 * @description
 * Internationalization (i18n) configuration and initialization.
 *
 * **Features:**
 * - Multi-language support (English, German)
 * - Automatic language detection from browser/localStorage
 * - Persistent language preference storage
 * - Namespace-based translation organization
 * - Debug access via window.i18next
 *
 * **Namespaces:**
 * - `translation` - General application translations
 * - `help` - Help modal and documentation
 *
 * **Supported Languages:**
 * - `en` - English (fallback)
 * - `de` - Deutsch (German)
 *
 * **Detection Order:**
 * 1. localStorage (persisted user preference)
 * 2. Browser language settings (navigator)
 *
 * @module i18n
 * @requires i18next
 * @requires react-i18next
 * @requires i18next-browser-languagedetector
 */
 
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
 
import en from './locales/en.json';
import de from './locales/de.json';
import helpEn from './locales/help_en.json';
import helpDe from './locales/help_de.json';
 
declare global {
  interface Window {
    i18next: typeof i18n;
  }
}
 
const resources = {
  en: { translation: en, help: helpEn },
  de: { translation: de, help: helpDe },
};
 
i18n
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    resources,
    fallbackLng: 'en',
    ns: ['translation', 'help'],
    defaultNS: 'translation',
    interpolation: {
      escapeValue: false,
    },
    detection: {
      order: ['localStorage', 'navigator'],
      caches: ['localStorage'],
    },
  });
 
const storedLanguage = localStorage.getItem('language') || 'en';
Eif (i18n.language !== storedLanguage) {
  i18n.changeLanguage(storedLanguage);
}
 
window.i18next = i18n;
 
export default i18n;