简体   繁体   English

如何在代码中使用Angular 2 Translate服务管道?

[英]How to use Angular 2 Translate service pipe in code?

Here is the thing... I'm using an angular 4 template in my app, and this has a good working translate service. 这就是事情...我在我的应用程序中使用了angular 4模板,它具有良好的翻译服务。 The problem is that I don't know how to use that pipe in code. 问题是我不知道如何在代码中使用该管道。

In HTML just put <span>{{ this.variable | traslate }}</span> 在HTML中,只需将<span>{{ this.variable | traslate }}</span> <span>{{ this.variable | traslate }}</span> and then, the service go to some JSON files to find the text and translate. <span>{{ this.variable | traslate }}</span> ,然后该服务转到一些JSON文件以查找文本并进行翻译。

Here is the code in my component.ts 这是我的component.ts中的代码

const SMALL_WIDTH_BREAKPOINT = 960;
@Component({
  selector: 'app-mycomponent',
  templateUrl: './mycomponent.component.html',
  styleUrls: ['./mycomponent.component.scss']
})

export class TrackingComponent implements OnInit { 
  currentLang = 'es';
  private mediaMatcher: MediaQueryList = matchMedia(`(max-width: 
  ${SMALL_WIDTH_BREAKPOINT}px)`);
  constructor(
    public translate: TranslateService,
    zone: NgZone,
    private router: Router,
  ) {
    const browserLang: string = translate.getBrowserLang();
      translate.use(browserLang.match(/en|es/) ? browserLang : 'es');
      this.mediaMatcher.addListener(mql => zone.run(() => {
        this.mediaMatcher = mql;
      }));
      translate.use(this.currentLang);
  }

  ngOnInit() { }

  myFunction(msg: string) : {
    const translatedText = msg; // HERE IS WHERE I WANT TO USE THE TRANSLATE SERVICE BUT I DON'T KNOW HOW TO CALL IT
    alert(translatedText );
  }

}

Yes, I put all the needed things in the module, because it works in HTML... but I don't know how to call it in the code. 是的,我将所有需要的东西都放在了模块中,因为它可以在HTML中工作...但是我不知道如何在代码中调用它。

I was searching how to solve my problem and I found This: 我正在寻找解决问题的方法,结果发现:

 let translatedText = new TranslatePipe().transform(msg);

 but the `TranslatePipe` does not work. 

Anyone knows how to call it? 有人知道怎么称呼吗?

You have basically 3 choices 您基本上有3 choices

If you are absolutely sure that your translation files are already loaded and you don't want to update the translations automatically if a language changes use 如果您完全确定您的翻译文件已经加载,并且您不想在语言更改时自动更新翻译,请使用

translate.instant('ID') translation.instant('ID')

If you are not sure about the loading state but you don't need updates on language changes use translate.get('ID'). 如果您不确定加载状态,但不需要更新语言,请使用translate.get('ID')。 It gives you an observable that returns the translation once it's loaded and terminates the observable. 它为您提供了一个可观察的对象,可在翻译加载后返回翻译并终止该可观察对象。

If you want constant updates (eg when a language changes) use translate.stream('ID') - it returns an observable that emits translation updates. 如果您想不断更新(例如,当一种语言更改时),请使用translate.stream('ID')-它返回一个可观察的内容,该内容发出翻译更新。 Make sure to dispose the observable if you don't need it anymore. 如果不需要它,请确保将其丢弃。

This assumes that you've injected TranslationService as translate in your component. 假设您已经在组件中注入了TranslationService作为翻译。

Eg 例如

export class AppComponent {
    constructor(translate: TranslateService) {
        translate.get('hello.world').subscribe((text:string) => {console.log(text});
    }
}

You'll have to assign the translation to your data array from within the subscribe 您必须在订阅中将转换分配给数据数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM