首页 > 解决方案 > Ngx-translate setTraslation 在动态组件中缺少翻译

问题描述

我想加载定义自己的翻译的动态组件,但仍使用 TranslateService 的相同实例,以便全局语言更改事件到达动态组件。

我正在运行一个我认为是错误的问题,请参阅此 stackblitz 进行复制:https ://stackblitz.com/edit/github-1ujje5

以及 github 上的相关问题:https ://github.com/ngx-translate/core/issues/834

任何建议如何解决这个问题?

标签: angularngx-translate

解决方案


发生这种情况是因为来自 http 请求的翻译覆盖了动态组件中设置的翻译。https://github.com/alexzuza在链接的 github 问题中提供的答案。

this.translate.getTranslation('en').pipe(take(1)).subscribe(() => {
  this.translate.setTranslation('en', {
    'DYNAMIC': 'I am dynamic component'
  }, true);
});

推荐阅读