使用@ ngx-translate

时间:2018-10-09 21:23:14

标签: angular typescript

我需要编辑@ ngx-translate / core库的管道,以了解哪些词没有被翻译并保存以将其发送到单独的服务中

export class MyMissingTranslationHandler implements MissingTranslationHandler {
    handle (params: MissingTranslationHandlerParams) {
        if (params.translateService.currentLang === params.translateService.defaultLang) {
            return 'not translate';
        }
    }
  }

@NgModule ({
    imports: [
        BrowserModule,
        HttpClientModule,
        BrowserAnimationsModule,
        RouterModule.forRoot (appRoutes),
        SharedModule,
        FuseMainModule,
        TranslateModule.forRoot ({
            loader: {
                provide: TranslateLoader,
                useFactory: HttpLoaderFactory,
                deps: [HttpClient]
            },
            missingTranslationHandler: {provide: MissingTranslationHandler, useClass: MyMissingTranslationHandler}
        }),

此刻,这导致在没有翻译的单词中将其替换为“不翻译”,而我需要的是捕获无法翻译的单词。

我试图修改文件translate.pipe.d.ts,但我不知道如何获得该值。非常感谢能帮助我的人

1 个答案:

答案 0 :(得分:1)

好的,就在这里。按照评论中的说明使用MissingTranslationHandler

@Injectable()
export class MyMissingTranslationHandler implements MissingTranslationHandler {
  handle(params: MissingTranslationHandlerParams) {
    this.nts.notTranslated(params.key);
    return '[MISSING]' + params.key;
  }

  constructor(private nts: NotTranslatedService) {}
}

使用以下方法初始化TranslateModule

TranslateModule.forRoot ({
        loader: {
            provide: TranslateLoader,
            useFactory: HttpLoaderFactory,
            deps: [HttpClient]
        },
        missingTranslationHandler: {
            provide: MissingTranslationHandler,
            useClass: MyMissingTranslationHandler,
            deps: [NotTranslatedService]
        }
    })

...以及服务:

@Injectable({
  providedIn: 'root'
})
export class NotTranslatedService {
  ...
  notTranslated(key: string) {
     console.log('Not translated', key);
     // do whatever you want...
  }
}