Uecko_ERP/packages/i18n/src/i18n.ts
2025-12-01 20:14:12 +01:00

58 lines
1.2 KiB
TypeScript

import i18n from "i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import { initReactI18next } from "react-i18next";
i18n
.use(LanguageDetector)
.use(initReactI18next)
.init({
detection: { order: ["navigator"] },
debug: false,
fallbackLng: "es",
interpolation: { escapeValue: false },
});
export { i18n };
/*export const initI18Next = () => {
if (hasInit) return i18next;
i18next
.use(LanguageDetector)
.use(initReactI18next)
.init({
detection: { order: ["navigator"] },
debug: true,
fallbackLng: "es",
interpolation: { escapeValue: false },
});
hasInit = true;
return i18next;
};*/
/**
* Registra dinámicamente traducciones de un módulo.
*
* Cada módulo tendrá su propio namespace.
*
* idempotente: si el bundle ya existe, no se vuelve a añadir.
*/
export const registerTranslations = (
moduleName: string,
locale: string,
resources: Record<string, unknown>
): void => {
/*if (!hasInit) {
throw new Error("i18n not initialized. Call initI18Next() first.");
}*/
const ns = moduleName;
const alreadyExists = i18n.hasResourceBundle(locale, ns);
if (!alreadyExists) {
i18n.addResourceBundle(locale, ns, resources, true, true);
}
};