Angular ngx-translate getTranslation 问题

Mat*_*Eon 6 ngx-translate angular

我正在使用 ngx-translate 来翻译我的 Angular Web 应用程序,但似乎 ngx-translate 的功能存在问题getTranslation(language)。当它被调用时,它会改变当前的语言?暂时?然后我的组件没有以正确的语言显示。

export class InputRadioComponent extends FormComponentInput implements OnInit {
  constructor(protected formDynamicS) {
  }

  public ngOnInit() {
    this.translate.getTranslation("fr").subscribe(res => {
      this.choose["fr"] = res['form-component']['choose-answer'];
    });
    this.translate.getTranslation("en").subscribe(res => {
      this.choose["en"] = res['form-component']['choose-answer'];
    });
    this.translate.getTranslation("de").subscribe(res => {
      this.choose["de"] = res['form-component']['choose-answer'];
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,就像this.translate.getTranslation("de")最后一次调用一样,我的组件始终以德语显示。我找到了一种解决方法,但这不是我想保留在我的代码中的东西。这是我的解决方法:

let languages: string[] = ["fr", "en", "de"];

languages.splice(languages.indexOf(this.translate.currentLang));
languages.push(this.translate.currentLang);

languages.forEach((language) => {
  this.translate.getTranslation(language).subscribe(res => {
    this.choose[language] = res['form-component']['choose-answer'];
  });
});
Run Code Online (Sandbox Code Playgroud)

它允许我保留 currentLang,因为它将是最后一次调用 getTranslation

Jov*_*n28 3

我刚才遇到了同样的问题。我不得不使用cloneDeep(lodash方法)来解决这个问题。

const translateDeepCopy = cloneDeep(this.translate);

translateDeepCopy.getTranslation(lang).subscribe(res => {
  
});
Run Code Online (Sandbox Code Playgroud)