gpt4 book ai didi

angular - @ngx-translate 可观察到不会因语言更改而触发

转载 作者:行者123 更新时间:2023-12-05 01:01:27 25 4
gpt4 key购买 nike

将 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);

最佳答案

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

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

关于angular - @ngx-translate 可观察到不会因语言更改而触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44678907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com