gpt4 book ai didi

angular - Transloco 翻译第一次不起作用

转载 作者:行者123 更新时间:2023-12-02 02:46:02 30 4
gpt4 key购买 nike

这是我的 Transloco 配置:

@Injectable({ providedIn: 'root' })
export class TranslocoHttpLoader implements TranslocoLoader {
constructor(private http: HttpClient) {}

getTranslation(lang: string) {
return this.http.get<Translation>(`/assets/i18n/${lang}.json`);
}
}

@NgModule({
exports: [ TranslocoModule ],
providers: [
{
provide: TRANSLOCO_CONFIG,
useValue: translocoConfig({
availableLangs: ['en', 'es'],
defaultLang: 'en',
// Remove this option if your application doesn't support changing language in runtime.
reRenderOnLangChange: true,
prodMode: environment.production,
})
},
{ provide: TRANSLOCO_LOADER, useClass: TranslocoHttpLoader }
]
})
export class TranslocoRootModule {}

当我尝试使用翻译方法进行翻译时,它第一次不起作用。

constructor(
private translate: TranslocoService
) {
console.log('<< ', this.translate.translate('dashboard.label'));
}

如果我通过路由器移动到另一条路线并返回,文本就会被翻译。似乎您第一次加载应用程序时没有时间加载翻译。有没有办法来解决这个问题?提前致谢。

最佳答案

正如文档所述:

Note that in order to safely use this method, you are responsible forensuring that the translation files have been successfully loaded bythe time it's called. If you aren't sure, you can use theselectTranslate() method instead

此方法返回一个可观察值:

translocoService.selectTranslate('hello').subscribe(value => ...)
translocoService.selectTranslate('hello', { value: 'world' }).subscribe(value => ...)
translocoService.selectTranslate('hello', {}, 'en').subscribe(value => ...)

Official docs

关于angular - Transloco 翻译第一次不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62777654/

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