fix: add detailed logging to i18n loading process

This commit is contained in:
gnezim
2026-04-06 10:15:21 +03:00
parent 2c1d706e0f
commit 221c7ed2c7
+34 -23
View File
@@ -49,15 +49,47 @@ const getInitialLanguage = (): string => {
const lng = getInitialLanguage() const lng = getInitialLanguage()
// Initialize i18n with empty resources (will be loaded dynamically) // Load language files synchronously
const loadLanguageSync = (lang: string): Record<string, string> => {
try {
const xhr = new XMLHttpRequest()
xhr.open('GET', `/assets/i18n/${lang}.json`, false) // synchronous
xhr.send()
console.log(`Loaded ${lang}: status=${xhr.status}, length=${xhr.responseText?.length}`)
if (xhr.status === 200) {
const data = JSON.parse(xhr.responseText)
const flattened = flattenTranslations(data)
console.log(`Flattened ${lang}: ${Object.keys(flattened).length} keys`)
return flattened
}
console.warn(`Failed to load ${lang} translations: status ${xhr.status}`)
return {}
} catch (error) {
console.error(`Error loading ${lang}:`, error)
return {}
}
}
// Create resources synchronously
const resources: Record<string, { translation: Record<string, string> }> = {}
const supportedLanguages = ['ru', 'en', 'es', 'fr', 'it', 'ja', 'ko', 'de', 'zh']
for (const lang of supportedLanguages) {
resources[lang] = { translation: loadLanguageSync(lang) }
}
// Initialize i18n
i18n i18n
.use(initReactI18next) .use(initReactI18next)
.init({ .init({
resources,
fallbackLng: 'ru', fallbackLng: 'ru',
lng, lng,
defaultNS: 'translation', defaultNS: 'translation',
ns: ['translation'], ns: ['translation'],
resources: {},
interpolation: { interpolation: {
escapeValue: false, escapeValue: false,
}, },
@@ -66,25 +98,4 @@ i18n
}, },
}) })
// Load language files
const loadLanguage = async (lang: string) => {
try {
const response = await fetch(`/assets/i18n/${lang}.json`)
if (!response.ok) {
throw new Error(`Failed to fetch ${lang}.json: ${response.status}`)
}
const data = await response.json()
const flattened = flattenTranslations(data)
i18n.addResourceBundle(lang, 'translation', flattened, true, true)
} catch (error) {
console.error(`Error loading ${lang} translations:`, error)
}
}
// Load all supported languages
const supportedLanguages = ['ru', 'en', 'es', 'fr', 'it', 'ja', 'ko', 'de', 'zh']
for (const lang of supportedLanguages) {
loadLanguage(lang)
}
export default i18n export default i18n