fix: use static translations in i18n configuration instead of async loading
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import i18n from 'i18next'
|
import i18n from 'i18next'
|
||||||
import { initReactI18next } from 'react-i18next'
|
import { initReactI18next } from 'react-i18next'
|
||||||
|
import { translations } from './translations'
|
||||||
|
|
||||||
// Language detection logic
|
// Language detection logic
|
||||||
const getInitialLanguage = (): string => {
|
const getInitialLanguage = (): string => {
|
||||||
@@ -43,17 +44,14 @@ const flattenTranslations = (obj: any, prefix = ''): any => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const lng = getInitialLanguage()
|
const lng = getInitialLanguage()
|
||||||
const supportedLanguages = ['ru', 'en', 'es', 'fr', 'it', 'ja', 'ko', 'de', 'zh']
|
|
||||||
|
|
||||||
// Build resources object by fetching all language files
|
// Build resources object with static translations
|
||||||
const resources: Record<string, any> = {}
|
const resources: Record<string, any> = {}
|
||||||
|
for (const [lang, translationData] of Object.entries(translations)) {
|
||||||
// Pre-populate with empty resources to avoid warnings
|
resources[lang] = { translation: flattenTranslations(translationData as any) }
|
||||||
for (const lang of supportedLanguages) {
|
|
||||||
resources[lang] = { translation: {} }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize i18next with empty resources
|
// Initialize i18next with bundled translations
|
||||||
i18n
|
i18n
|
||||||
.use(initReactI18next)
|
.use(initReactI18next)
|
||||||
.init({
|
.init({
|
||||||
@@ -70,26 +68,4 @@ i18n
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
// Load and add translations asynchronously
|
|
||||||
const loadAllTranslations = async () => {
|
|
||||||
for (const lang of supportedLanguages) {
|
|
||||||
try {
|
|
||||||
const response = await fetch(`/assets/i18n/${lang}.json`)
|
|
||||||
if (response.ok) {
|
|
||||||
const data = await response.json()
|
|
||||||
const flattened = flattenTranslations(data)
|
|
||||||
// Use addResourceBundle to add the translations AFTER initialization
|
|
||||||
i18n.addResourceBundle(lang, 'translation', flattened, true, true)
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`[i18n] Error loading ${lang}:`, error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
loadAllTranslations().then(() => {
|
|
||||||
// Change language after all translations are loaded
|
|
||||||
i18n.changeLanguage(lng)
|
|
||||||
})
|
|
||||||
|
|
||||||
export default i18n
|
export default i18n
|
||||||
|
|||||||
Reference in New Issue
Block a user