Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据URL语言Angular2翻译管道_Angular_Typescript_Pipe - Fatal编程技术网

根据URL语言Angular2翻译管道

根据URL语言Angular2翻译管道,angular,typescript,pipe,Angular,Typescript,Pipe,我在Angular2应用程序中有一个转换管道。 我有一个名为:LanguageComponent的组件,它是所有其他组件的父组件 { path: ':lang', component: LanguageComponent, children: [ { path: 'search', component: SearchComponent }, ]} 语言组件读取URL并将其添加到翻译服务和从此服务读取的管道: this.route.params.subscrib

我在Angular2应用程序中有一个转换管道。 我有一个名为:LanguageComponent的组件,它是所有其他组件的父组件

{ path: ':lang', component: LanguageComponent,
    children: [
      { path: 'search', component: SearchComponent },   
    ]}
语言组件读取URL并将其添加到翻译服务和从此服务读取的管道:

this.route.params.subscribe(params => {
   this.translate.setCurrentLanguage(params['lang'].toLowerCase());
});
public defaultLanguage = 'en-us';
getCurrentLanguage(): any {
   if (this.locale == null) {
     this.locale = window.navigator.language == null ? this.defaultLanguage : window.navigator.language;
   }
   return this.locale;
}
一切正常,translate和params中的值正确。 但问题出在GUI中:

{{ 'label' | translate }}
管道在语言组件之前编译。因此,“标签”的值总是默认值:“en us”

翻译服务:

this.route.params.subscribe(params => {
   this.translate.setCurrentLanguage(params['lang'].toLowerCase());
});
public defaultLanguage = 'en-us';
getCurrentLanguage(): any {
   if (this.locale == null) {
     this.locale = window.navigator.language == null ? this.defaultLanguage : window.navigator.language;
   }
   return this.locale;
}
路由返回正确的值。但是如果我在语言组件中设置了一个调试器onInit,在语言服务中设置了另一个调试器onInit。服务中的调试器将在另一个调试器之前启动,并将读取默认值,无论URL中的值是什么


有人知道怎么解决吗

您可以构建类似Angular的东西

管道的构造函数应该注入private _ref:changedtectorref

transform方法应将转换后的值存储在实例变量中,并返回引用实例变量的WrappedValue


它还应该从语言服务订阅当前语言。当当前语言更改时,它应该更新实例变量,然后调用它。_ref.markForCheck这将导致重新计算包装的值。

我不太明白。我加了零钱。但一切都没有改变。你说WrappedValue是什么意思。你能给我提供代码解决方案吗?非常感谢。不,我无法提供代码解决方案。我现在没有时间。我上面链接的异步管道实现应该是一个很好的工作模板。好的,没问题,谢谢你的想法。但我没有订阅。那么我应该在哪里使用它呢?\u ref.markForCheck?您应该更改您的语言服务,以便您可以订阅当前的语言。谢谢,它可以工作。在我调用路由之后,我做了_ref.markForCheck,它工作了。