用Angular2翻译

时间:2018-04-26 06:33:42

标签: angular

我想要翻译文字,我总是使用{{ 'Name' | translate }} in my html 但现在我需要使用翻译我的.ts,但我有一个问题。

    this.gridOptions = <GridOptions>{};

    this.columnDefs = [
      {
        headerName: {{'Name' | translate }},
        field: 'name'
      }];

Forbbiden bitwise operation (no-bitwise)
Not found the name translate
hope ','.
hope ';'.

由于

2 个答案:

答案 0 :(得分:2)

您可以使用ngx-translate

constructor(private translate: TranslateService) {
  this.translate.setDefaultLang('en');

  // Initially setting the language as the browser's lang
  if (this.translate.getBrowserLang() !== undefined) {
     this.translate.use(this.translate.getBrowserLang());
  } else {
     this.translate.use('en');
  }
}

generateColDef() {

  // 'APP.NAMESTR' is the key to your Names translation which resides in your language json
  this.translate.get('APP.NAMESTR').subscribe(value => {
    this.columnDefs = [{
       headerName: value,
       field: 'name'
     }]; 

  });
}

有关更完整的示例,请查看此stackblitz

答案 1 :(得分:1)

您必须在.ts文件中使用Pipe,如下所示:

import {Pipename} from './pipename';

Pipename.prototype.transform(arguments);

无需在{{}}文件中使用.ts