【发布时间】:2018-06-05 17:51:49
【问题描述】:
我正在使用 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'];
});
}
}
在这种情况下,就像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'];
});
});
它允许我保留 currentLang,因为这将是 getTranslation 的最后一次调用
【问题讨论】:
标签: angular ngx-translate