i18next / i18next-xhr-backend

[deprecated] can be replaced with i18next-http-backend
https://github.com/i18next/i18next-http-backend
MIT License
253 stars 75 forks source link

Unable to load multiple files #318

Open ashitvora-zz opened 5 years ago

ashitvora-zz commented 5 years ago

I am able to load a single locale file by setting the path in loadPath but was wondering if there's a way to load multiple files by returning an array from loadPath function.

The use case is to load the translation file along with momentjs's locale file.

jamuhl commented 5 years ago

the xhr-backend loads file based on lng-ns --> so loading multiple namespaces in i18next will also load multiple files. See: https://www.i18next.com/how-to/add-or-load-translations#load-using-a-backend-plugin

ashitvora-zz commented 5 years ago

Moment and Numeral provide locale config js files whereas i18next expects resource file in JSON format.

What I did in my init options is... ns: ['common',moment/${i18next.language},numeral/${i18next.language}],

and here's what the loadPath function looks like...

loadPath: (lng: string, ns: string) => {
    return `/locales/${lng}/${ns}.js`;
}

but obviously, this does not work. Is there a better option than this?

jamuhl commented 5 years ago

we did a lazy load of moment locales triggered by => https://www.i18next.com/overview/api#onlanguagechanged

ashitvora-zz commented 5 years ago

onLanguageChanged gets called after i18next.changeLanguage is called and by that time the language has already been changed. Any event which can stop re-rendering till the moment, numeral locales are loaded.

jamuhl commented 5 years ago

how you trigger rerender?

jamuhl commented 5 years ago

languageChanging event is called before...but still no guarantee that languageChanged does not be called before your moment locales got loaded --> best make some custom function to changeLanguage loading those first and than call i18next.changeLanguage