要在懒加载模块中使用Angular ngx-translate,您需要执行以下步骤:
npm install @ngx-translate/core @ngx-translate/http-loader --save
import { TranslateService } from '@ngx-translate/core';
export class LazyModule {
constructor(private translate: TranslateService) {
// 设置默认语言
translate.setDefaultLang('en');
// 添加语言支持
translate.use('en');
}
}
{{ 'TEXT_TO_TRANSLATE' | translate }}
TranslateService
的getTranslation
方法加载翻译文件。例如:import { TranslateService } from '@ngx-translate/core';
const routes: Routes = [
{
path: '',
component: LazyComponent,
resolve: {
translate: TranslateService,
},
},
];
TranslateModule
和TranslateLoader
。例如:import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
@NgModule({
imports: [
// ...
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient],
},
}),
],
// ...
})
export class AppModule { }
这样,您就可以在懒加载模块中使用Angular ngx-translate了。请确保您已正确设置翻译文件,并将其放置在正确的位置。