Files
xenia-pos-local/packages/i18n/src/i18n.js
T
bonaminandClaude Opus 5.5 cc4e76019b chore: deliberate dependency upgrades; drop legacy-peer-deps; Capacitor 8.6.2 (shell build 2)
Platform-v2 step 2.99c.
- Both apps on the same current versions: React 19.3.1, react-query 5.107.1,
  router 7.18.5, axios 1.21.1, zustand 5.1, vite 8.3.8, plugin-react 6.3; manager
  lucide-react 1.14 -> 1.73, recharts 3.8 -> 3.10.
- i18next 27.1.1 + react-i18next 18.0.1 (exact pins). Shared options moved to
  packages/i18n/src/options.js with returnEmptyString: true (27 shows the key for an
  empty text otherwise); tests/instance.test.mjs guards it, plus fallback, code
  casing and plurals.
- vite-plugin-pwa 2.0 lists Vite 8 as a peer: .npmrc (legacy-peer-deps) removed,
  `npm install` resolves without it.
- Native (CS-10): @capacitor/core/android/cli 8.6.2, @capacitor/app 8.1.2,
  @capgo/capacitor-updater 8.54.2; versionCode 2 / versionName 1.1.0 and
  MIN_SHELL_BUILD 2. capacitor-updater 8.54 refuses plain-HTTP downloads by
  default, which broke every bundle check in the stacks suite: httpsOnly: false in
  capacitor.config.json (venues have no domains; zips are sha256-checked).
- npm audit: 6 left, all in workbox-build 7.4.1 (build time).
Full run-all incl. Android passes (Android re-run after the httpsOnly fix).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 13:22:51 +02:00

47 lines
1.9 KiB
JavaScript

import i18next from 'i18next'
import { initReactI18next } from 'react-i18next'
import { FALLBACK_LANGUAGE, SUPPORTED_LANGUAGES, normalizeLanguage } from './locale.js'
import { NAMESPACES, resources } from './resources.js'
import { BASE_OPTIONS } from './options.js'
const DEV = typeof import.meta !== 'undefined' && !!import.meta.env?.DEV
/**
* One i18next instance per app, ready synchronously (resources are bundled).
*
* const i18n = createI18n({ language: resolveLanguage({ venue: 'el' }) })
* <I18nProvider i18n={i18n} venue={bootstrap.venue}>…</I18nProvider>
*
* A key missing in the current language falls back to English. A key missing in English
* too renders the key itself and, in dev builds, warns in the console (and
* scripts/check-i18n.mjs fails on it before it gets that far).
* `extra` adds resources ({ el: { myns: {...} } }) on top of the bundled ones.
*/
export function createI18n({ language = FALLBACK_LANGUAGE, defaultNS = 'common', extra } = {}) {
const i18n = i18next.createInstance()
i18n.use(initReactI18next).init({
resources: mergeResources(resources, extra),
lng: normalizeLanguage(language) || FALLBACK_LANGUAGE,
...BASE_OPTIONS,
ns: NAMESPACES,
defaultNS,
saveMissing: DEV,
missingKeyHandler: DEV ? (lngs, ns, key) => console.warn(`[i18n] missing key "${ns}:${key}" (${lngs.join(', ')})`) : undefined,
react: { useSuspense: false },
})
const setHtmlLang = lng => { if (typeof document !== 'undefined') document.documentElement.lang = lng }
setHtmlLang(i18n.language)
i18n.on('languageChanged', setHtmlLang)
return i18n
}
function mergeResources(base, extra) {
if (!extra) return base
const out = { ...base }
for (const [lng, namespaces] of Object.entries(extra)) {
out[lng] = { ...(out[lng] || {}) }
for (const [ns, data] of Object.entries(namespaces)) out[lng][ns] = { ...(out[lng][ns] || {}), ...data }
}
return out
}