【发布时间】:2019-11-11 00:39:31
【问题描述】:
我正在关注这个关于 Angular 翻译的教程 (https://alligator.io/angular/ngx-translate/)。当我在 HTML 文件中调用翻译时,什么也没发生,我看到一个空行:
<label translate='demo.title'></label>
但在我的 component.ts 中,如果我导入 import {TranslateService} from '@ngx-translate/core';,我可以使用以下方法获得正确的翻译:
title:string;
constructor(private translate: TranslateService) { }
ngOnInit() {
this.translate.get(['demo.title'])
.subscribe(translations => {
this.title = translations['demo.title'];
console.log(this.title ); // the right translations appears in console
});
}
app.module.ts
// import ngx-translate and the http loader
import {TranslateLoader, TranslateModule} from '@ngx-translate/core';
import {TranslateHttpLoader} from '@ngx-translate/http-loader';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AppRoutingModule,
HomeModule,
IndexModule,
NoPageModule,
HttpClientModule,
// ngx-translate and the loader module
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
}),
RouterModule.forRoot(routes, { useHash: true }),
],
providers: [AuthService, AuthGuard],
bootstrap: [AppComponent],
schemas:[CUSTOM_ELEMENTS_SCHEMA]
})
export class AppModule {}
// required for AOT compilation
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
pt.json
{
"demo": {
"title": "oi",
"text": "This is a simple demonstration app for ngx-translate"
}
}
关于这个话题,我怎么知道Angular这个浏览器语言?
最好的问候
【问题讨论】:
标签: angular ngx-translate