打字稿中的角度ngx-translate用法 [英] Angular ngx-translate usage in typescript

查看:170
本文介绍了打字稿中的角度ngx-translate用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此@ ngx-translate/core i18n服务,它在使用以下语法的模板(.html)中可以正常工作:

I'm using this @ngx-translate/core i18n service and it works fine in templates (.html) with this syntax:

{{'my.i18n.key'| translate}}

现在,我想翻译组件打字稿文件(.ts)中的某些内容,但我不知道如何使用它.

Now I want to translate something in my component typescript file (.ts) but I don't know how to use it.

我可以在构造函数中创建翻译对象:

I can create translate object in my constructor:

构造函数(私人翻译:TranslateService){}

以及现在如何翻译"my.i18n.key"?

and now how to translate 'my.i18n.key' ?

推荐答案

来自 github :

get(key:string | Array,interpolateParams ?: Object): 可观察的:获取键(或 键数组)或键(如果找不到该值)

get(key: string|Array, interpolateParams?: Object): Observable: Gets the translated value of a key (or an array of keys) or the key if the value was not found

尝试您的控制器/类:

constructor(private translate: TranslateService) {
    let foo:string = this.translate.get('myKey');
}

这篇关于打字稿中的角度ngx-translate用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆