Files
3x-ui/frontend/src/i18n/react.ts
T
Sanaei 0abbe1a4af chore(deps): update frontend dependencies
Majors: i18next 26 -> 27.1.1 with react-i18next 17 -> 18.0.1, and jsdom
30 -> 31. The vitest group moves together to 5.1.0 (vitest, coverage-v8,
browser-playwright and the addon-vitest override). Also antd 6.7.0,
storybook 10.6.7, vite 8.3.8, @vitejs/plugin-react 6.3.0, react-query
5.107.1, react-hook-form 7.92.0, msw 3.0.5, playwright 1.65.0, otpauth
9.6.0 and oxfmt 0.78.0.

i18next 27 dropped interpolation.nestingPrefixEscaped, the option that kept
"$t(...)" nesting from ever matching, and an empty nestingPrefix falls back
to "$t(". The prefix is now a NUL character, which no translation or
node-reported text contains; node-list-untrusted-i18n.test.tsx goes red
without it.

Held back: oxlint stays at 1.90.0, because 1.91 and later flag six
fetch-in-effect loaders with react(set-state-in-effect) and those need a
data-loading rewrite of their own. @hookform/resolvers 7.0.6 and
oxlint-tsgolint 7.0.2007 are less than a day old, so 7.0.5 and 7.0.2006
are locked; the lockfile was resolved with a one-day cutoff.
2026-11-12 19:26:56 +01:00

64 lines
2.0 KiB
TypeScript

import i18next from 'i18next';
import type { InterpolationOptions } from 'i18next';
import { initReactI18next } from 'react-i18next';
import { LanguageManager } from '@/utils';
import type { LanguageScope } from '@/utils';
import { loadAntdLocale } from './AntdRootConfig';
import enUS from '../../../internal/web/translation/en-US.json';
const FALLBACK = 'en-US';
// No locale uses $t(...) nesting, and untrusted text (node-reported names, remarks) must not
// reach it; i18next 27 has no off switch, so the prefix is a NUL that no text contains.
export const INTERPOLATION: InterpolationOptions = {
escapeValue: false,
prefix: '{',
suffix: '}',
nestingPrefix: '\u0000',
};
const lazyModules = import.meta.glob([
'../../../internal/web/translation/*.json',
'!../../../internal/web/translation/en-US.json',
]);
function moduleKeyFor(code: string): string {
return `../../../internal/web/translation/${code}.json`;
}
export async function readyI18n(scope: LanguageScope = 'panel') {
let active = LanguageManager.getLanguage(scope);
if (
active !== FALLBACK &&
!Object.prototype.hasOwnProperty.call(lazyModules, moduleKeyFor(active))
) {
active = FALLBACK;
}
await i18next.use(initReactI18next).init({
lng: active,
fallbackLng: FALLBACK,
resources: { [FALLBACK]: { translation: enUS } },
interpolation: INTERPOLATION,
returnNull: false,
});
if (active !== FALLBACK) {
const loader = lazyModules[moduleKeyFor(active)] as
| (() => Promise<{ default: Record<string, unknown> }>)
| undefined;
if (loader) {
const mod = await loader();
const messages = (mod.default ?? mod) as Record<string, unknown>;
i18next.addResourceBundle(active, 'translation', messages, true, true);
await i18next.changeLanguage(active);
}
}
// The html files ship lang="en"; screen readers pick their voice from it (s-ui#1293).
document.documentElement.lang = i18next.language;
await loadAntdLocale(i18next.language);
return i18next;
}
export { i18next as i18n };