在组件中使用 ngx-translate [英] Use ngx-translate in component
问题描述
我使用 ngx-translate 在视图中没有问题,使用管道.我需要做的是在组件中使用它,例如显示错误消息,或定义数据表列默认内容.
I'm using ngx-translate with no-problems in views, using pipes. What i need to do is to use it in a component, for example to show an error message, or define a datatable column default content.
我正在尝试执行以下操作:
I'm trying to do something like:
translate.instant("AREA.NEW");
或
translate.get("AREA.NEW").subscribe((res: string) => {
console.log(res);
});
我试过在 ngOnInit() 和 ngAfterViewInit() 中调用它
I've tried calling it in ngOnInit() and ngAfterViewInit()
但在这两种情况下,我只得到AREA.NEW",而不是翻译后的词.我假设 json 字典是在我调用后加载的,所以我没有意识到它是如何工作的.
But in both cases i just get "AREA.NEW", not the translated word. I asume the json dictionary is loaded after my call, so i don't realize how make it work.
推荐答案
导入 TranslateService 并随时随地使用.
Import the TranslateService and use it wherever you want.
import { TranslateService } from '@ngx-translate/core';
export class YourComponent {
constructor(private translateService: TranslateService) {
console.log('translation', this.translateService.instant('my_i18n_json_defined_key'));
}
}
这篇关于在组件中使用 ngx-translate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!