Quantcast
Channel: Ionic Forum - Latest posts
Viewing all articles
Browse latest Browse all 228595

Anyone tried the Translate service?

$
0
0

Hi,

I have successfully implemented ng2-translate to Ionic2 project. Here are the steps you need to do:

  1. npm install ng2-translate --save
  2. open you app.js and import service like this:

    import {TranslateService, TranslatePipe} from 'ng2-translate/ng2-translate';

  3. 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);
}

  1. put your en.json (for english language) in /www/assets/i18n/ (my example)
  2. use this in template:

{{'FOO' | translate}}

  1. 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 :wink:


Viewing all articles
Browse latest Browse all 228595

Trending Articles