Core: Access translate from .ts file (Angular2)

Created on 28 Nov 2017  路  7Comments  路  Source: ngx-translate/core

how to access translate pipe from .ts file in angular2
Please provide with import statements required for this.

Most helpful comment

see this
https://stackoverflow.com/a/50754530/3417228

To translate something in your typescript file, do the following

constructor(private translate: TranslateService) {}
then use like this wherever you need to translate

this.translate.instant('my.i18n.key')

All 7 comments

I have the same question because i want to display in alertCtrl a message that depends on the language.
I can get language in the .ts and display the right message but i search another better way.

this.translateService.get('DASHBOARD_TITLE') .subscribe(value => { this.dashboardTitle = value; } );

see this
https://stackoverflow.com/a/50754530/3417228

To translate something in your typescript file, do the following

constructor(private translate: TranslateService) {}
then use like this wherever you need to translate

this.translate.instant('my.i18n.key')

@sundaramkumar the above does not work for me :

(picture of the app displaying the notification with "notification.login.defaultError.body" instead of the retrieved value)

@jmruiz6 your solution is async I need a sync solution.

@ngehlert, @ocombe how do I solve this ?

here's my code :

  LoginErrorMessage(){
    const myConf = {...conf};
    myConf.timeout = 3500;
    const body = this.translate.instant('notification.login.defaultError.body');
    const title = this.translate.instant('notification.login.defaultError.title');
    this.snotifyService.error(body, title, myConf);
  }

fr.json :

{
"notifications" : {
    "login":{
      "defaultError":{
        "title":"Connexion 脡chou茅",
        "body":"La connexion a 茅chou茅 pour une raison inconnue.",
        "config":{
          "timeout":null
        }
      },
   }
}

I'm using the snotify library for notifications, any ideas?

@tatsujb what is your error, i don't quite get it. if you don't receive a translation well probably your file is not loaded

instant(key: string|Array, interpolateParams?: Object): string|Object: Gets the instant translated value of a key (or an array of keys). /! This method is synchronous and the default file loader is asynchronous. You are responsible for knowing when your translations have been loaded and it is safe to use this method. If you are not sure then you should use the get method instead.

const title = this.translate.instant('notification.login.defaultError.title');

Thank you, it works for me

@tatsujb I know it's late but in your json file it's wrote "notifications", not "notification".
You forgot the 's' haha.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

webprofusion-chrisc picture webprofusion-chrisc  路  4Comments

louisdoe picture louisdoe  路  3Comments

bjornharvold picture bjornharvold  路  3Comments

apreg picture apreg  路  3Comments

pndewit picture pndewit  路  3Comments