【问题标题】:ngx-translate .instant() doesn't load is not instantngx-translate .instant() 不加载不是即时的
【发布时间】:2021-11-09 10:12:21
【问题描述】:

我翻译我在本地获得的数据,它们不是异步数据。

我的原生文本来自我的文件V2VehiculeOutput.StatusEnum中导入的枚举

但如果我想看翻译,我不得不重新加载页面。

我尝试使用下面的代码从 ngOnInit 和构造函数加载翻译,但它没有改变任何东西。

    Object.keys(V2VehiculeOutput.StatusEnum).forEach(s => {
      console.log(this.translate.instant('FILTERS.VEHICULE.STATUS.' + V2VehiculeOutput.StatusEnum[s]));
      });
    });

我阅读了那个问题https://github.com/ngx-translate/core/issues/517,但我的数据甚至不是异步的......那么为什么 translate.instant 不直接加载我的数据?

我错过了什么吗?

【问题讨论】:

  • 您能否分享一下您是如何加载翻译的,通过HttpTranslateLoader 或手动通过addLangs 方法?你在哪里初始化defaultLang,use呢?

标签: javascript angular angular8 ngx-translate


【解决方案1】:

这篇帖子 https://www.digitalocean.com/community/tutorials/angular-ngx-translate 帮助了我。

现在使用这个例子似乎可以正常工作。

this.translate.get(['login.username', 'login.password'])
      .subscribe(translations => {
        this.usernameLabel = translations['login.username'];
        this.passwordLabel = translations['login.password'];
});

这个错误并不是每次加载都出现,所以我会密切关注,但目前感觉很好。

【讨论】:

    猜你喜欢
    • 2018-02-23
    • 2021-06-09
    • 2018-04-15
    • 1970-01-01
    • 2019-11-28
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多