import environment from './environment';
import {PLATFORM} from 'aurelia-pal';
import 'babel-polyfill';
import * as Bluebird from 'bluebird';
import {I18N, TCustomAttribute} from 'aurelia-i18n';
import Backend from 'i18next-xhr-backend';
// remove out if you don't want a Promise polyfill (remove also from webpack.config.js)
Bluebird.config({ warnings: { wForgottenReturn: false } });
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.feature(PLATFORM.moduleName('resources/index'));
// Uncomment the line below to enable animation.
// aurelia.use.plugin(PLATFORM.moduleName('aurelia-animator-css'));
// if the css animator is enabled, add swap-order="after" to all router-view elements
// Anyone wanting to use HTMLImports to load views, will need to install the following plugin.
// aurelia.use.plugin(PLATFORM.moduleName('aurelia-html-import-template-loader'));
if (environment.debug) {
aurelia.use.developmentLogging();
}
if (environment.testing) {
aurelia.use.plugin(PLATFORM.moduleName('aurelia-testing'));
}
aurelia.use.plugin(**PLATFORM.moduleName('aurelia-i18n')**, (instance) => {
let aliases = ['t', 'i18n'];
// add aliases for 't' attribute
TCustomAttribute.configureAliases(aliases);
// register backend plugin
instance.i18next.use(Backend); **// Backend is not defined**
// 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 : 'no',
fallbackLng : 'en',
debug : false
});
});
aurelia.start().then(() => aurelia.setRoot(PLATFORM.moduleName('app')));
}
app.js:
import {I18N} from 'aurelia-i18n';
import {inject} from 'aurelia-framework';
@inject(I18N)
export class App {
constructor(i18N){
this.i18N = i18N;
console.log(this.i18n.tr('msg')); **//tr is undefined**
}
You are using the Backend Loader provided by the plugin but you installed the xhr-i18next-plugin. So you should import the backend from there and wire it. Take a look at the plugin docs. The provided backend from aurelia really depends on the loader capability as such its not the right fit for Webpack.
I got the plugin working by importing the Backend from aurelia-i18n, since I have no reason to use i18next-xhr-backend in the first place.
For the record: Create the project
same as before
Install dependencies
yarn add aurelia-i18n
Create the translation files
src/locations folder must be renamed to src/locales, to be aligned with the code
main.js :
////CHANGES FROM PREVIOUS main.js uses 4 slashes like so ////
import environment from ‘./environment’;
import {PLATFORM} from ‘aurelia-pal’;
import ‘babel-polyfill’;
import * as Bluebird from ‘bluebird’;
////IMPORTING Backend FROM aurelia-i18n
import {Backend, TCustomAttribute} from 'aurelia-i18n';
////REMOVED import Backend from 'i18next-xhr-backend';
// remove out if you don't want a Promise polyfill (remove also from webpack.config.js)
Bluebird.config({ warnings: { wForgottenReturn: false } });
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.feature(PLATFORM.moduleName('resources/index'));
// Uncomment the line below to enable animation.
// aurelia.use.plugin(PLATFORM.moduleName('aurelia-animator-css'));
// if the css animator is enabled, add swap-order="after" to all router-view elements
// Anyone wanting to use HTMLImports to load views, will need to install the following plugin.
// aurelia.use.plugin(PLATFORM.moduleName('aurelia-html-import-template-loader'));
if (environment.debug) {
aurelia.use.developmentLogging();
}
if (environment.testing) {
aurelia.use.plugin(PLATFORM.moduleName('aurelia-testing'));
}
aurelia.use.plugin(PLATFORM.moduleName('aurelia-i18n'), (instance) => {
let aliases = ['t', 'i18n'];
// add aliases for 't' attribute
TCustomAttribute.configureAliases(aliases);
// register backend plugin
//// BACKEND IS CHANGED
//// old line: instance.i18next.use(Backend);
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
////**JSON LOADER DOES NOT SUPPORT './' IN PATH **
//// old line: loadPath: './locales/{{lng}}/{{ns}}.json', //<-- XHR settings for where to get the files from
loadPath: 'locales/{{lng}}/{{ns}}.json', //<-- XHR settings for where to get the files from
},
attributes: aliases,
lng : 'no',
fallbackLng : 'en',
debug : true
});
});
aurelia.start().then(() => aurelia.setRoot(PLATFORM.moduleName('app')));
}