【问题标题】:@ngx-translate get observable not firing on language change@ngx-translate 可观察到不会因语言更改而触发
【发布时间】:2017-07-04 00:14:43
【问题描述】:

将 angular2 与 @ngx-translate 模块一起使用

我根据用户选择进行语言更改并使用以下代码设置语言:

this.translate.use('en');

在其他组件的其他地方,我使用 .get observable 来检索一些标签。

this.translate.get(element.bucketId.toUpperCase()).subscribe( x => { bucket.bucketLabel = x; } );

当前语言的初始标签加载正常,但执行 .use 时,标签不会更改为新语言。 observable 不会触发。

要解决这个问题,我必须执行以下操作:

this.translate.get(element.bucketId.toUpperCase()).subscribe( x => { bucket.bucketLabel = x; } );
this.translate.onLangChange.subscribe( event => {
    bucket.bucketLabel = this.translate.instant(element.bucketId.toUpperCase());
});

这显然显得过分了。

使用管道和指令的代码完美地切换语言。

我是否误解了 .get 的作用?还是在进行语言切换时我还缺少其他一些技巧。不仅仅是做 this.translate.use(lang);

【问题讨论】:

    标签: angular ngx-translate


    【解决方案1】:

    试试.stream()方法,当语言改变时它会返回新值。

    https://github.com/ngx-translate/core#methods

    【讨论】:

    • this.translate.stream('DASHBOARD.LOGOUT');提出:“TranslateService”类型上不存在属性“流”
    • 节省了很多时间...竖起大拇指
    猜你喜欢
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 2019-09-13
    • 2019-09-29
    • 1970-01-01
    • 2021-01-09
    相关资源
    最近更新 更多