Angular中的多语言

  • 多语言文件
    • 位置:assets/i18n/xxx.json,如en.json
    • 可以在app.module.ts文件中指定多语言文件路径
registerLocaleData(localeDe, 'de-DE', localeDe);
registerLocaleData(localeZh, 'zh-CN', localeZh);

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    CoreModule,
    SharedModule,
    OverviewModule,
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: (createTranslateLoader),
        deps: [HttpClient]
      },
      isolate: false
    }),
    AppRoutingModule
  ],
  providers: [
    { provide: ErrorHandler, useClass: GlobalErrorHandler },
    { provide: HTTP_INTERCEPTORS, useClass: ServerErrorInterceptor, multi: true },
    { provide: HTTP_INTERCEPTORS, useClass: LoggingInterceptor, multi: true },
  ],
  bootstrap: [AppComponent],
  schemas: [
    CUSTOM_ELEMENTS_SCHEMA
  ]
})
export class AppModule { }

export function createTranslateLoader(http: HttpClient) {
  return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}
  • 数据绑定
    • 如<div class="h6">{{'XXXPage.notConfigured' | translate}}</div>,会自动去assets/i18n/下面的当前语言的文件中找对应key的值
上一篇:vue-i18n 初体验


下一篇:使用vue国际化中出现内置的组件无法切换语言的问题(element-ui、ivew)