feat(i18n): @xenia/i18n translations, language resolution and venue-time formatting
Platform-v2 step 2.4 (APPS_PLATFORM D-11, D-12, §7.2, §8): - i18next 26.7.2 + react-i18next 17.1.13 (exact); en/el locale files (common, dev); createI18n, I18nProvider, useLanguage, setUserLanguage (screen first, then PATCH /api/auth/me through an app-provided save) - resolveLanguage: user -> device -> venue -> en - useFormat(): dates/times always in the venue zone from bootstrap (24 h, DST-safe, business dates never shifted), relative times, money and numbers per language; parseNumber accepts a comma or a dot - scripts/check-i18n.mjs guard: keys missing in en, placeholder mismatches, dates formatted without the venue zone in new code (fails); missing el (warns) - @xenia/ui components read their texts from UiLabelsProvider; useUiLabels() supplies the translated set - dev-only /i18n-sample in the manager; ui.mjs e2e checks it with the browser in New York; unit tests in run-all; lockfile keeps both apps' versions - new pack rule CS-17 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import i18next from 'i18next'
|
||||
import { initReactI18next } from 'react-i18next'
|
||||
import { FALLBACK_LANGUAGE, SUPPORTED_LANGUAGES, normalizeLanguage } from './locale.js'
|
||||
import { NAMESPACES, resources } from './resources.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,
|
||||
fallbackLng: FALLBACK_LANGUAGE,
|
||||
supportedLngs: SUPPORTED_LANGUAGES,
|
||||
ns: NAMESPACES,
|
||||
defaultNS,
|
||||
fallbackNS: 'common',
|
||||
interpolation: { escapeValue: false }, // React escapes already
|
||||
returnNull: false,
|
||||
initAsync: false,
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user