Today I tried to install i18n-translation in my aurelia-cli project. I did the steps described in the aurelia i18n guide.
But now I’m getting an error in chrome browser vendor-bundle.js:34158 Uncaught Error: Dynamic load not allowed: locales/de/translation.json.
Does anyone now why this error occurs and how I can fix that?
I don’t use webpack (only requirejs).
Follownig the plugin configuration (didn’t change anything):
.plugin('aurelia-i18n', (instance) => {
let aliases = ['t', 'i18n'];
// add aliases for 't' attribute
TCustomAttribute.configureAliases(aliases);
// register backend plugin
instance.i18next.use(Backend.with(aurelia.loader));
// adapt options to your needs (see http://i18next.com/docs/options/)
// make sure to return the promise of the setup method, in order to guarantee proper loading
return instance.setup({
backend: { // <-- configure backend settings
loadPath: './locales/{{lng}}/{{ns}}.json', // <-- XHR settings for where to get the files from
},
attributes: aliases,
lng: 'de',
fallbackLng: 'en',
debug: false
});
})
Hey, again many thanks for your help. I got it working. The trick for me was to change
import Backend from 'i18next-xhr-backend';
to
import * as Backend from 'i18next-xhr-backend';
Then the following main.ts config works:
.plugin('aurelia-i18n', (instance) => {
let aliases = ['t', 'i18n'];
// add aliases for 't' attribute
TCustomAttribute.configureAliases(aliases);
// register backend plugin
instance.i18next.use(Backend);
// adapt options to your needs (see http://i18next.com/docs/options/)
// make sure to return the promise of the setup method, in order to guarantee proper loading
return instance.setup({
backend: { // <-- configure backend settings
loadPath: './locales/{{lng}}/{{ns}}.json', // <-- XHR settings for where to get the files from
},
attributes: aliases,
lng: 'de',
fallbackLng: 'en',
debug: false
});
});
The xhr backend uses ajax to dynamically load json files (not through requirejs), instead of bundling them. You have to make sure those json files were copied to right places when deploy to production site.
The json-processor would look into the src folder. But according to i18n guide, the json files are saved in root (same level as src).
Maybe this could be a reason. But now I’m using the xhr-backend what works for me.
Second, create a folder named locales in your project’s root.
This is where the doc is confusing, it means project root as defined in aurelia.json which is src folder in your repo.
If you further look down the doc, you will see mention of src/locales files.
I remember early version of i18n indeed talked about translation files outside of src folder and dynamically loading them. To me, dynamical loading fit the nature of i18n, who wants to load all possible lanaguages upfront while user is gonna use just one of them.
The current default setup with default loader and default json bundling is biased to easiness than technical best choice.
Since you are using dynamic loading now, there is no reason to go back to default setup.