首页 > 解决方案 > NgxTranslate 不适用于 HTTP 拦截器

问题描述

我正在使用 ngxtranslate 翻译应用程序。它就像魅力一样。问题是我有 HttpInterceptor,它一开始就试图从本地存储中获取数据,如果询问您是否要加载最后的更改或关闭,它会显示一个对话框。
问题是对话框中的文本没有翻译,我不知道该怎么做,因为它无处不在。
HTTP拦截器文件

import { TranslateService } from '@ngx-translate/core';

        @Injectable()
        export class CacheInterceptor implements HttpInterceptor {
        
          constructor(private cacheService: HttpCacheService, private dialog: MatDialog, private translate: TranslateService) {}
        
          intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
            return from(this.handle(req, next));
          }
         .......
            const cachedResponse: HttpResponse<any> = 
             this.cacheService.get(reqId);
             if (cachedResponse) {
              console.log('cache hit');

              const dialogResult = await this.openDialog();
              if (dialogResult) {
              console.log('Loading item from cache');
              // return modifyied response
            .........
               async openDialog() {
                  const dialogData = new ConfirmationDialogModel(
                  await this.translate.instant('some.text'),
                  await this.translate.instant('some.anotherText')
               );
             ....

输出是公正的some.textsome.anotherText 我该怎么做才能让它工作?

标签: javascriptangulartypescripttranslatengx-translate

解决方案


你试试看?

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
openDialog()
return from(this.handle(req, next));
}

推荐阅读