简体   繁体   English

Angular ngx-translate 内部服务

[英]Angular ngx-translate inside service

I am using ngx-translate and ngx-translate/http-loader for translating my ionic/angular project.我正在使用 ngx-translate 和 ngx-translate/http-loader 来翻译我的 ionic/angular 项目。 I have this code inside app.module.ts imports:我在 app.module.ts 导入中有这段代码:

TranslateModule.forRoot({
  loader: {
    provide: TranslateLoader,
    useFactory: (createTranslateLoader),
    deps: [HttpBackend]
  }
})

and this loader function in the same file:和这个加载器函数在同一个文件中:

export function createTranslateLoader(handler: HttpBackend) {
     const http = new HttpClient(handler);
     return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

This gives me opportunity to use this kind of translation syntac inside component's html : {{ 'TEXT' | translate }}这让我有机会在组件的 html 中使用这种翻译语法: {{ 'TEXT' | translate }} {{ 'TEXT' | translate }} , I also can write something like that to translate inside component's ts file : var text = this.translateService.instant("TEXT"); {{ 'TEXT' | translate }} ,我也可以写这样的东西来翻译组件的 ts 文件: var text = this.translateService.instant("TEXT");

Now I want to use translateService.instant inside service generated by command "ng generate s" I tried it but it doesn't work, it returns "TEXT" itself.现在我想在由命令"ng generate s"服务内部使用translateService.instant我试过了,但它不起作用,它本身返回"TEXT" So what is the problem?那么问题是什么?

try this.translateService.get("TEXT").subscribe(...)试试this.translateService.get("TEXT").subscribe(...)

see: https://github.com/ngx-translate/core#4-use-the-service-the-pipe-or-the-directive见: https ://github.com/ngx-translate/core#4-use-the-service-the-pipe-or-the-directive

instant(key: string|Array, interpolateParams?: Object): string|Object: Gets the instant translated value of a key (or an array of keys). instant(key: string|Array, interpolateParams?: Object): string|Object: 获取键(或键数组)的即时转换值。 /!\ This method is synchronous and the default file loader is asynchronous. /!\ 这个方法是同步的,默认的文件加载器是异步的。 You are responsible for knowing when your translations have been loaded and it is safe to use this method.您有责任知道您的翻译何时已加载,并且使用此方法是安全的。 If you are not sure then you should use the get method instead.如果您不确定,那么您应该使用 get 方法。

如果我理解正确,那么在您新生成的服务中,您只需注入翻译服务:

constructor(translateService: TranslateService) {}

First inject the translate service in your service constructor:首先在你的服务构造函数中注入翻译服务:

constructor(public translateService: TranslateService) {}

And then use it to get the translation from current language:然后用它来获取当前语言的翻译:

this.translateService.getTranslation(this.translateService.currentLang).subscribe((translation: string) => { this.translationText= translation.text; });

I had the same issue, but i add to the constructor of the service generated by the command the language and fix it to me.我遇到了同样的问题,但是我将语言添加到命令生成的服务的构造函数中并将其修复给我。

constructor(
    private translate: TranslateService,
  ) {
    this.translate.use(environment.lang);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM