将参数应用于翻译服务和字符串数组

Apply param with the translate service and array of strings

这是from the doc:

translate.get('HELLO', {value: 'world'}).subscribe((res: string) => {
    console.log(res);
    //=> 'hello world'
});

如何应用数组?

 setPayPal(): void {
    this.translateService.get(['Client.Pay-With-PayPal', 'Client.Please-Click-And-Make-The-Payment',
     ]).subscribe(res => {
        this.payPal = {
          title: res['Client.Pay-With-PayPal'],
          payPalUrl: environment.payPalUrl,
          description: res['Client.Please-Click-And-Make-The-Payment'] 
        };
      });
  }

我可以这样得到那个值:value: environment.parcelDeliveryCost

gr.json

"Please-Click-And-Make-The-Payment":"Bitte anklicken und die Bezahlung von {{value}} vornehmen",

所以我的问题是如何将它应用于数组?

楼主的回答

setPayPal(): void {

    forkJoin([this.translateService.get(['Client.Pay-With-PayPal', 'Client.Pay-Now']),
    this.translateService.get('Client.Please-Click-And-Make-The-Payment', { value: environment.parcelDeliveryCost })])
      .subscribe(([res1, res2]) => {
        this.payPal = {
          title: res1['Client.Pay-With-PayPal'],
          payPalUrl: environment.payPalUrl,
          description: res2,
          parentButtonText: res1['Client.Pay-With-PayPal'],
          childButtonText: res1['Client.Pay-Now'],
        };
      });
  }

原答案

您可以根据可观察对象的性质和需求,使用 RxJS forkJoincombineLatestzip 方法触发多个可观察对象。尝试以下

forkJoin({
  'Client.Pay-With-PayPal': this.translateService.get('Client.Pay-With-PayPal'),
  'Client.Please-Click-And-Make-The-Payment': this.translateService.get('Client.Please-Click-And-Make-The-Payment')
}).subscribe(
  response => {
    this.payPal = {
      title: response['Client.Pay-With-PayPal'],
      payPalUrl: environment.payPalUrl,
      description: response['Client.Please-Click-And-Make-The-Payment'] 
    };
  },
  error => {
    // handle error
  }
);

请注意,forkJoin 仅在所有可观察对象完成时发出。如果它们是持久性可观察对象并且您希望仅使用第一个发出的值,则可以使用 combineLatest 方法并在 take(1) 运算符中使用管道。

还有一件事,虽然 forkJoin 有一个重载来接受普通对象作为参数(如上所示),但 combineLatestzip 没有它们。参数应该是一个数组。