Files
myeasycms-v2/packages/i18n/src/i18n.client.ts
Giancarlo Buomprisco f3ac595d06 MCP Server 2.0 (#452)
* MCP Server 2.0

- Updated application version from 2.23.14 to 2.24.0 in package.json.
- MCP Server improved with new features
- Migrated functionality from Dev Tools to MCP Server
- Improved getMonitoringProvider not to crash application when misconfigured
2026-02-11 20:42:01 +01:00

91 lines
2.6 KiB
TypeScript

import i18next, { type InitOptions, i18n } from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import resourcesToBackend from 'i18next-resources-to-backend';
import { initReactI18next } from 'react-i18next';
// Keep track of the number of iterations
let iteration = 0;
// Maximum number of iterations
const MAX_ITERATIONS = 20;
/**
* Initialize the i18n instance on the client.
* @param settings - the i18n settings
* @param resolver - a function that resolves the i18n resources
*/
export async function initializeI18nClient(
settings: InitOptions,
resolver: (lang: string, namespace: string) => Promise<object>,
): Promise<i18n> {
const loadedLanguages: string[] = [];
const loadedNamespaces: string[] = [];
await i18next
.use(
resourcesToBackend(async (language, namespace, callback) => {
const data = await resolver(language, namespace);
if (!loadedLanguages.includes(language)) {
loadedLanguages.push(language);
}
if (!loadedNamespaces.includes(namespace)) {
loadedNamespaces.push(namespace);
}
return callback(null, data);
}),
)
.use(LanguageDetector)
.use(initReactI18next)
.init(
{
...settings,
showSupportNotice: false,
detection: {
order: ['cookie', 'htmlTag', 'navigator'],
caches: ['cookie'],
lookupCookie: 'lang',
cookieMinutes: 60 * 24 * 365, // 1 year
cookieOptions: {
sameSite: 'lax',
secure:
typeof window !== 'undefined' &&
window.location.protocol === 'https:',
path: '/',
},
},
interpolation: {
escapeValue: false,
},
},
(err) => {
if (err) {
console.error('Error initializing i18n client', err);
}
},
);
// to avoid infinite loops, we return the i18next instance after a certain number of iterations
// even if the languages and namespaces are not loaded
if (iteration >= MAX_ITERATIONS) {
console.debug(`Max iterations reached: ${MAX_ITERATIONS}`);
return i18next;
}
// keep component from rendering if no languages or namespaces are loaded
if (loadedLanguages.length === 0 || loadedNamespaces.length === 0) {
iteration++;
console.debug(
`Keeping component from rendering if no languages or namespaces are loaded. Iteration: ${iteration}. Will stop after ${MAX_ITERATIONS} iterations.`,
);
throw new Error('No languages or namespaces loaded');
}
return i18next;
}