在TypeScript文件中翻译Angular语言。

3
public typeArray= [
       {
          id: 'MOTHER',
          name: '{{ myApp.type.MOTHER | translate }}'     
       }];

在TypeScript文件中定义数组时,我们如何编写翻译?

2个回答

2

您可以通过导入管道在组件或任何需要的地方使用。将您的翻译管道导入到组件中,然后添加到构造函数中。

注:本文中的“管道”指的是Angular框架中的“管道(pipe)”对象。
constructor(private yourPipe: YourPipe) {}

或者您可以从您的管道类中创建一个新实例:

public yourPipe: YourPipe = new YourPipe();

那么您可以像这样使用它:
this.yourPipe.transform(value);

transform函数将通过管道返回转换后的值。

所以在你的情况下:

   public typeArray = [
       {
         id: 'MOTHER',
         name: this.yourPipe.transform(myApp.type.MOTHER)
       }
   ];

0
你需要从 '@ngx-translate/core' 导入 TranslateService 并使用它的 get 方法。
import { TranslateService } from '@ngx-translate/core';

constructor(private translateService: TranslateService) {}

method() {
    this.translateService.get(myApp.type.MOTHER).subscribe((mother) => 
    {
        let typeArray= [{
            id: 'MOTHER',
            name: mother     
        }];
        ...
    });
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接