Angular 更新至V16版后,支援 standalone,故移除了 NgModule,而TranslateModule 又要在AppModule中 import,那该如何做呢?
npm install @ngx-translate/core
npm install @ngx-translate/http-loade
// import ngx-translate and the http loader
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { HttpClient, HttpClientModule } from '@angular/common/http';
// required for AOT compilation
export function HttpLoaderFactory(http: HttpClient): TranslateHttpLoader {
return new TranslateHttpLoader(http);
}
bootstrapApplication(AppComponent, {
providers: [
importProvidersFrom(
NzMessageServiceModule,
NzDrawerServiceModule,
NzModalModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
)
]
}).catch(err => console.error(err));
For example: i18n\en.json, i18n\zh_CN.json, i18n\zh_TW.json
For example: en.json
{
"test": {
"title": "This is a test title."
}
}
zh_CN.json
{
"test": {
"title": "这是一个测试标题."
}
}
zh_TW.json
{
"test": {
"title": "這是一個測試標題."
}
}
import { TranslateModule, TranslateService } from '@ngx-translate/core';
...
imports: [NzButtonModule, NzSelectModule, TranslateModule, CommonModule, FormsModule, NzCardModule, NzFormModule, NzInputModule]
...
constructor(private translate: TranslateService) {
this.translate.setDefaultLang('en');
this.translate.use('en');
this.selectedLang = 'en';
}
{{ 'test.title' | translate }}
const lang = this.translate.instant(key);
changeLanguage(): void {
let lang = this.translate.getDefaultLang();
this.translate.setDefaultLang(this.selectedLang);
this.translate.use(this.selectedLang);
}
import { CommonModule } from '@angular/common';
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzCardModule } from 'ng-zorro-antd/card';
import { NzFormModule } from 'ng-zorro-antd/form';
import { NzInputModule } from 'ng-zorro-antd/input';
import { NzSelectModule } from 'ng-zorro-antd/select';
@Component({
selector: 'app-pages-sample-translate',
template: `
{{ 'test.title' | translate }}
`,
styles: [
`
nz-select {
width: 200px;
}
`
],
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
providers: [],
imports: [NzButtonModule, NzSelectModule, TranslateModule, CommonModule, FormsModule, NzCardModule, NzFormModule, NzInputModule]
})
export class TranslateComponent implements OnInit {
selectedLang: any = {};
constructor(private translate: TranslateService) {
this.translate.setDefaultLang('en');
this.translate.use('en');
this.selectedLang = 'en';
}
ngOnInit(): void {}
changeLanguage(): void {
let lang = this.translate.getDefaultLang();
this.translate.setDefaultLang(this.selectedLang);
this.translate.use(this.selectedLang);
}
log(value: any): void {
this.changeLanguage();
}
getLang(key: string): void {
const lang = this.translate.instant(key);
console.log(lang);
}
}