gpt4 book ai didi

Angular ngx-translate 内部服务

转载 作者:搜寻专家 更新时间:2023-10-30 21:46:08 24 4
gpt4 key购买 nike

我正在使用 ngx-translate 和 ngx-translate/http-loader 来翻译我的 ionic/angular 项目。我在 app.module.ts 导入中有这段代码:

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

和这个加载器函数在同一个文件中:

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

这让我有机会在组件的 html 中使用这种翻译语法:{{ 'TEXT' | translate }} ,我也可以写一些类似的东西来翻译组件的 ts 文件: var text = this.translateService.instant("TEXT");

现在我想在命令 "ng generate s" 生成的服务中使用 translateService.instant 我试过了,但它不起作用,它返回 “TEXT” 本身。那么问题是什么?

最佳答案

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

参见: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). /!\ 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.

关于Angular ngx-translate 内部服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54592537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com