Arreglo de idiomas en producción

This commit is contained in:
David Arranz 2025-12-01 19:52:19 +01:00
parent 8b5678da5a
commit 07e873bb36
4 changed files with 22 additions and 22 deletions

View File

@ -3,13 +3,12 @@
"version": "0.0.0",
"private": true,
"type": "module",
"sideEffects": false,
"scripts": {
"typecheck": "tsc -p tsconfig.json --noEmit",
"clean": "rimraf .turbo node_modules dist"
},
"exports": {
".": "./src/index.ts"
".": "./src/i18n.ts"
},
"dependencies": {
"i18next": "25.6.0",

View File

@ -1,10 +1,20 @@
import i18next, { type i18n } from "i18next";
import i18n from "i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import { initReactI18next } from "react-i18next";
let hasInit = false;
i18n
.use(LanguageDetector)
.use(initReactI18next)
.init({
detection: { order: ["navigator"] },
debug: true,
fallbackLng: "es",
interpolation: { escapeValue: false },
});
export const initI18Next = () => {
export { i18n };
/*export const initI18Next = () => {
if (hasInit) return i18next;
i18next
@ -12,14 +22,14 @@ export const initI18Next = () => {
.use(initReactI18next)
.init({
detection: { order: ["navigator"] },
debug: false,
debug: true,
fallbackLng: "es",
interpolation: { escapeValue: false },
});
hasInit = true;
return i18next;
};
};*/
/**
* Registra dinámicamente traducciones de un módulo.
@ -33,25 +43,16 @@ export const registerTranslations = (
locale: string,
resources: Record<string, unknown>
): void => {
if (!hasInit) {
/*if (!hasInit) {
throw new Error("i18n not initialized. Call initI18Next() first.");
}
}*/
const ns = moduleName;
const alreadyExists = i18next.hasResourceBundle(locale, ns);
const alreadyExists = i18n.hasResourceBundle(locale, ns);
if (!alreadyExists) {
i18next.addResourceBundle(locale, ns, resources, true, true);
console.log("cargando => ", moduleName, locale);
i18n.addResourceBundle(locale, ns, resources, true, true);
}
};
/**
* Acceso al `t()` global por si se necesita en librerías de backend.
*/
export const t = (...args: Parameters<i18n["t"]>) => {
if (!hasInit) {
throw new Error("i18n not initialized. Call initI18Next() first.");
}
return i18next.t(...args);
};

View File

@ -1 +0,0 @@
export * from "./i18n";

View File

@ -23,6 +23,7 @@
},
"devDependencies": {
"@biomejs/biome": "^2.3.1",
"@repo/i18next": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@tailwindcss/postcss": "^4.1.5",
"@turbo/gen": "^2.5.2",