无法在Http拦截器和主组件Angular 2、Angular 6中使用Ng2 Translate转换密钥

无法在Http拦截器和主组件Angular 2、Angular 6中使用Ng2 Translate转换密钥,angular,angular-http-interceptors,ng2-translate,Angular,Angular Http Interceptors,Ng2 Translate,我的代码如下,我使用第三方登录(openAM)为我的应用程序。当我得到401时,我应该将应用程序重定向到另一个URL,用户将从此处登录 我使用http拦截器检查状态并重定向到登录。 问题1:这种方法正确吗 问题2:翻译没有发生 检查handleHttpResponseCodes方法。在这种情况下,“未经授权的信息”不是翻译。不仅如此,任何信息都不会被翻译。我的代码有什么问题吗 从'@angular/core'导入{Injectable,Injector}; 从“@angular/common

我的代码如下,我使用第三方登录(openAM)为我的应用程序。当我得到401时,我应该将应用程序重定向到另一个URL,用户将从此处登录

我使用http拦截器检查状态并重定向到登录。 问题1:这种方法正确吗

问题2:翻译没有发生

检查handleHttpResponseCodes方法。在这种情况下,“未经授权的信息”不是翻译。不仅如此,任何信息都不会被翻译。我的代码有什么问题吗


从'@angular/core'导入{Injectable,Injector};
从“@angular/common/http”导入{HttpEvent、HttpInterceptor、HttpHandler、HttpRequest、HttpResponse、HttpErrorResponse};
从“rxjs/Observable”导入{Observable};
导入'rxjs/add/operator/do';
导入“rxjs/add/operator/catch”;
导入“rxjs/add/observable/throw”;
从'app/blocks/auth/auth.service'导入{AuthService};
从“ng2 translate”导入{TranslateService};
从“../../core/services/common”导入{AlertService};
从'@angular/Router'导入{Router};
从“toastr”导入*作为jsToaster;
@可注射()
导出类AppHttpInterceptor实现HttpInterceptor{
建造师(
专用注射器:注射器,
专用路由器:路由器,
私人翻译:翻译服务
) {}
截取(req:HttpRequest,next:HttpHandler):可观察的>{
//控制台日志(req);
如果(!window.navigator.onLine){
//检查一下是否有互联网
//如果没有internet,抛出HttpErrorResponse错误
//由于抛出错误,函数将在此处终止
返回可观察的抛出(新的HttpErrorResponse({
错误:“没有互联网”
}));
}否则{
//否则返回正常请求
返回此.handleResponse(下一步,req);
}
}
HandlerResponse(下一步,要求){
返回下一个句柄(req)
.do((ev:HttpEvent)=>{
if(HttpResponse的ev实例){
//log('do中的ev:',ev);
}
})
.catch((响应:任意)=>{
if(HttpErrorResponse的响应实例){
此。handleHttpResponseCodes(响应);
}否则{
日志(“::未知错误::”,响应);
}
返回可观察。抛出(响应);
});
}
handleHttpResponseCodes(异常){
让authService=this.injector.get(authService);
让translate=this.injector.get(TranslateService);
设alertService=this.injector.get(alertService);
const router=this.injector.get(路由器);
开关(异常状态){
案例401://未经授权
调试器;
this.translate.get('unauthorizedMessage').subscribe((res:string)=>{
调试器;
**//问题:翻译没有发生为什么//**
这是showtoos(res,'401');
});
设置超时(()=>{
authService.login_openAM();
}, 3000);
打破
案例400://请求错误或FR会话超时
this.translate.get('loginSessionExpiredMessage').subscribe((res:string)=>{
调试器;
这是showtoos(res,'400');
});
设置超时(()=>{
authService.loginSessionExpired();
}, 2000);
打破
案例403://禁止进入
//待办事项
打破
案例502://坏网关
打破
案例503://服务不可用
jsToaster.error(“服务不可用”,“503”);
打破
案例504://网关超时
jsToaster.error(“服务不可用”,“代码:504-网关超时”);
打破
}
log(“:服务异常::”,异常);
}
showToast(标题、正文、Toast类型、等待时间){
设置超时(()=>{
jsToaster[typeOfToast](正文,标题);
},等候时间);
}

}
此外,请尝试显式使用区域设置代码:

this.translate.use('en-EN');

基本上,你走的路是对的。 但拦截器应该是这样的:

import { Injectable } from '@angular/core';
import { HttpHandler, HttpEvent, HttpRequest, HttpErrorResponse, HttpInterceptor } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from "rxjs/operators";

@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).pipe(
      catchError((err: HttpErrorResponse, caught: Observable<any>) => {
        switch(err.status){
         case 404:
          return throwError('Errors.404');
        default:
          return throwError('Errors.500')
        }
      })
    );
  }
}
HTML
模板上,只需使用
translate
管道进行本地化:

<div>{{error|translate}}</div>
{{error | translate}

下面是一个

确保您设置了翻译服务并正确提供了拦截器。这对我来说很有用:

//app.module.ts
@NGD模块({
提供者:[{
提供:HTTP_拦截器,
useClass:AppHttpInterceptor,
多:是的,
deps:[授权服务、翻译服务、警报服务、路由器]
}],
// ...
})
导出类AppModule{
构造函数(translateService:translateService,localStorage:LocalStorageService){
translateService.addLangs(支持的语言);
translateService.setDefaultLang(默认语言);
//使用存储在浏览器本地存储中的lang。。。
const useLang:string=localStorage.getLang()
//…浏览器语言如果受支持。。。
||支持的_LANGS.find(lang=>lang==translate.getBrowserLang())
//…或默认语言
||(二);
使用(useLang)
.pipe(takeWhile(()=>this.componentActive)、switchMap(()=>translate.onLangChange))
//在语言更改时更新本地存储
.subscribe((e:TranslationChangeEvent)=>localStorage.setLang(e.lang));
}
// ...
}
//app-http.interceptor.ts
导出类AppHttpInterceptor实现HttpInterceptor{
构造函数(私有authService:authService,
<div>{{error|translate}}</div>