i18next: Problem using XHR backend with jquery-i18next
I’ve been having a lot of trouble getting i18next to work when using the XHR backend and jquery-i18next. I can get i18next + jquery-i18next to work when I add the translations as JSON to the page, but I need to have the translations in files. I get the same error for each key: i18next::translator: missingKey undefined translation [key]. My test en-US.json file is being successfully loaded. How do I handle this problem?
Here’s the code I’m using:
i18next.use(window.i18nextXHRBackend)
.init({
debug: true,
whitelist: ['en-US', 'es'],
fallbackLng: 'en-US',
backend: {
loadPath: 'langs/{{lng}}.json'
}
}, (err, t) => {
jqueryI18next.init(i18next, $);
$(document).localize();
});
Thanks in advance.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 24 (11 by maintainers)
I was using incorrect key. I forgot to change it after making changes for “allowMultiLoading: true”. it is working with the suggestions you made. Thanks for you help!
“i18next::translator: missingKey undefined translation key” looks like you call
t(undefinded)
can you paste your complete sample or create a jsbin?you server needs to return the same as you pass in resources.en.translation and you can’t have the route omitting the language and namespace -> it needs to load one file per language and namespace pair.
means
/api/Locales/en/translation
should returnif you like to load all translations in all languages via
/api/Locales
(not recommended as you load to much) you can setallowMultiLoading: true
in backend options https://github.com/i18next/i18next-xhr-backend and json has to look like:Okay, that fixed it. Thanks for all your help.
change your json to:
The loading route already has the language and namespace set - so no need to provide that in json files
Well, here’s the entire HTML page:
And here’s the structure of the language file: