Hi,
I have successfully implemented ng2-translate to Ionic2 project. Here are the steps you need to do:
npm install ng2-translate --save
-
open you app.js and import service like this:
import {TranslateService, TranslatePipe} from 'ng2-translate/ng2-translate';
-
Then update @App like this:
@App({
templateUrl: 'build/app.html',
config: {},
providers: [TranslateService, HeroesService]
pipes: [TranslatePipe]})
And constructor:
export class MyApp {
constructor(
platform: Platform,
app: IonicApp,
translate: TranslateService,
heroes: HeroesService
) {
this.platform = platform;
this.app = app;
this.translate = translate;
this.translationConfig();
this.initializeApp();
this.root = News;
}
initializeApp() {
this.platform.ready().then(() => {
console.log('Platform ready');
});
}
// TODO: implement ng2-translate when they'll fix
translationConfig() {
var prefix = 'assets/i18n/';
var suffix = '.json';
this.translate.useStaticFilesLoader(prefix, suffix);
var userLang = navigator.language.split('-')[0]; // use navigator lang if available
userLang = /(de|en)/gi.test(userLang) ? userLang : 'en';
// optional, default is "en"
this.translate.setDefaultLang('en');
// the lang to use, if the lang isn't available, it will use the current loader to get them
this.translate.use(userLang);
}
- put your en.json (for english language) in
/www/assets/i18n/
(my example) - use this in template:
{{'FOO' | translate}}
-
To use in your @Page *.js files, add this part of code to them:
import {TranslateService, TranslatePipe} from 'ng2-translate/ng2-translate';
import {Page, NavParams, NavController} from 'ionic/ionic';
@Page({
templateUrl: 'build/pages/News/News.html',
pipes: [TranslatePipe]
})
export class News {
constructor(
params: NavParams,
nav: NavController,
translate: TranslateService
) {
Job's done