how to access translate pipe from .ts file in angular2
Please provide with import statements required for this.
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.
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')