ngx-translate 在 Angular 7 中使用变量作为参数

ngx-translate using a variable as a parameter in Angular 7

我在使用 Angular7 中的 NGX-Translate 时遇到问题。

我正在尝试翻译带有参数的短语。如果参数是硬编码的,它可以工作,但如果参数是一个变量,它就不会。

app.component.ts

import { Component, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  hardcoded: string;
  fromVariable: string;
  days: '30';

  constructor(private translate: TranslateService) { }

  ngOnInit() {
    this.translate.setDefaultLang('en');
    this.translate.use('en');

   // Value Hardcoded - THIS WORKS
    this.translate.get('UPCOMING_RENEWALS', { output: '30' }).subscribe((s: string) => {
      this.hardcoded = s;
    });
    // value from variable - THIS DOESN'T
    this.translate.get('UPCOMING_RENEWALS', { output: this.days }).subscribe((s: string) => {
      this.fromVariable = s;
    });

  }
}

app.component.html

<h1>
    {{ 'UPCOMING_RENEWALS' | translate :{output:'30'} }}</h1>
outputs: Upcoming Renewals (30 days)


<h1>{{hardcoded}}</h1>
outputs: Upcoming Renewals (30 days)

<h1>{{fromVariable}}</h1>
outputs: Upcoming Renewals ({{output}} days)

en.json

{
  "UPCOMING_RENEWALS": "Upcoming Renewals ({{output}} days)",
}

这是 https://stackblitz.com/edit/angular-failing-translate-variable

上的示例

是因为days: '30'。你没有正确初始化 days,你只是将它的类型设置为 '30',这意味着你不能设置 days 除了 '30'

我想这是一个错字。将其更改为 days = '30'