import i18n from "i18next"; import { initReactI18next } from "react-i18next"; import Backend from "i18next-http-backend"; // 1. Функция для принудительного определения языка из URL const getLanguageFromURL = (): string => { const path = window.location.pathname; // Регулярка для /en/ /ru/ /uz/ в начале пути const langMatch = path.match(/^\/(en|ru|uz)(\/|$)/i); if (langMatch) { const detectedLang = langMatch[1].toLowerCase(); return detectedLang; } return "ru"; // Жёсткий фолбэк }; // 2. Конфиг с полной отладкой i18n .use(Backend) .use(initReactI18next) .init({ lng: getLanguageFromURL(), // Принудительно из URL fallbackLng: "ru", supportedLngs: ["en", "ru", "uz"], debug: true, interpolation: { escapeValue: false, }, backend: { loadPath: "/locales/{{lng}}.json", allowMultiLoading: false, requestOptions: { cache: "no-store", }, }, react: { useSuspense: false, // Отключаем для совместимости с React 18 }, detection: { order: ["path"], // Только из URL lookupFromPathIndex: 0, caches: [], // Не использовать localStorage }, parseMissingKeyHandler: (key) => { console.warn("⚠️ Main i18n: Missing translation:", key); return key; // Вернёт ключ вместо ошибки }, missingKeyHandler: (lngs, ns, key) => { console.error("🚨 Main i18n: Missing i18n key:", { key, languages: lngs, namespace: ns, stack: new Error().stack, // Выведет стек вызовов }); }, }) .catch((err) => { console.error("❌ Main i18n: Initialization failed:", err); }); // 3. Логирование всех событий i18n.on("languageChanged", (lng) => { console.log("🔄 Main i18n: Language changed to:", lng); }); i18n.on("initialized", (options) => { console.log("🎯 Main i18n: Initialized event fired with options:", options); }); i18n.on("failedLoading", (lng, ns, msg) => { console.error(`💥 Main i18n: Failed loading ${lng}.json:`, msg); // Если не удалось загрузить русский, пробуем английский if (lng === "ru") { console.log("🔄 Main i18n: Trying to load English as fallback"); i18n.changeLanguage("en"); } }); i18n.on("loaded", (loaded) => { console.log("📦 Main i18n: Translations loaded:", loaded); }); i18n.on("missingKey", (lngs, namespace, key, res) => { console.warn("⚠️ Main i18n: Missing key event:", { lngs, namespace, key, res }); }); export default i18n;