Uecko_ERP/packages/i18n/src/i18n.ts

59 lines
1.3 KiB
TypeScript
Raw Normal View History

2025-12-01 18:52:19 +00:00
import i18n from "i18next";
2025-11-28 15:00:18 +00:00
import LanguageDetector from "i18next-browser-languagedetector";
import { initReactI18next } from "react-i18next";
2025-12-01 18:52:19 +00:00
i18n
.use(LanguageDetector)
.use(initReactI18next)
.init({
detection: { order: ["navigator"] },
debug: true,
fallbackLng: "es",
interpolation: { escapeValue: false },
});
2025-11-28 15:00:18 +00:00
2025-12-01 18:52:19 +00:00
export { i18n };
/*export const initI18Next = () => {
2025-11-28 15:00:18 +00:00
if (hasInit) return i18next;
i18next
.use(LanguageDetector)
.use(initReactI18next)
.init({
detection: { order: ["navigator"] },
2025-12-01 18:52:19 +00:00
debug: true,
2025-11-28 15:00:18 +00:00
fallbackLng: "es",
interpolation: { escapeValue: false },
});
hasInit = true;
return i18next;
2025-12-01 18:52:19 +00:00
};*/
2025-11-28 15:00:18 +00:00
/**
* 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 => {
2025-12-01 18:52:19 +00:00
/*if (!hasInit) {
2025-11-28 15:00:18 +00:00
throw new Error("i18n not initialized. Call initI18Next() first.");
2025-12-01 18:52:19 +00:00
}*/
2025-11-28 15:00:18 +00:00
const ns = moduleName;
2025-12-01 18:52:19 +00:00
const alreadyExists = i18n.hasResourceBundle(locale, ns);
2025-11-28 15:00:18 +00:00
if (!alreadyExists) {
2025-12-01 18:52:19 +00:00
console.log("cargando => ", moduleName, locale);
i18n.addResourceBundle(locale, ns, resources, true, true);
2025-11-28 15:00:18 +00:00
}
};