Angular 2 i18n dynamic/instant translation

前端 未结 3 1030
萌比男神i
萌比男神i 2020-12-16 00:33

I\'ve followed the angular.io cookbook for internationalization (https://angular.io/docs/ts/latest/cookbook/i18n.html#!#angular-i18n). Everything works fine, and if I change

3条回答
  •  别那么骄傲
    2020-12-16 00:49

    You can certainly do this if you're using JIT compilation. You'll need a factory provider for your translations. Add something like this to your root module:

      export function localeFactory(): string {
        return (window.clientInformation && window.clientInformation.language) || window.navigator.language;
      }
    
      providers:
      [
        {
          provide: TRANSLATIONS,
          useFactory: (locale) => {
            locale = locale || 'en'; // default to english if no locale provided
            return require(`raw-loader!../locale/messages.${locale}.xlf`);
          },
          deps: [LOCALE_ID]
        },
        {
          provide: LOCALE_ID,
          useFactory: localeFactory
        },
        {provide: TRANSLATIONS_FORMAT, useValue: 'xlf'},
    

提交回复
热议问题