angular的国际化方案,采用ngx-translate来实现。
安装模块:
npm install @ngx-translate/core --save
在根模块中导入:
// other module
import {TranslateModule} from '@ngx-translate/core';
@NgModule({
declarations: [
AppComponent,
],
imports: [
// other module
TranslateModule.forRoot(),
],
providers: [
],
bootstrap: [AppComponent]
})
export class AppModule {
}
我们希望可以在一个固定的文件里面配置对应的翻译文件,然后在每个用到的组件里面使用它,随意我们需要借助TranslateHttpLoader来加载翻译文件。首先安装TranslateHttpLoader:
npm install @ngx-translate/http-loader --save
翻译文件可以放在/assets/i18n/[lang].json中,[lang]代表使用的语言文件名称。然后我们可以在跟组件中添加配置对应的加载项:
// other module
import {TranslateModule} from '@ngx-translate/core';
// 自定义加载方法