Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 5 change locale dynamically for i18n

I am trying to change locale dynamically to change i18n language. I have two files, the one with english values and the other one with french values.

What I've tried for now is something like that :

 ngOnInit() {
    const localeName = localStorage.getItem('locale') || 'fr';
    import(`@angular/common/locales/${localeName}.js`).then(locale => {
      registerLocaleData(locale.default);
    });
  }

but it gave me the following error :

error TS1323: Dynamic import cannot be used when targeting ECMAScript 2015 modules.

Any ideas on how I can switch from english to french dynamically? :/

like image 409
j0w Avatar asked Aug 06 '18 09:08

j0w


People also ask

How do I set the runtime locale of an angular application?

The Angular CLI automatically includes the locale data and sets the LOCALE_ID value when you use the --localize option with ng build command. To manually set the runtime locale of an application to one other than the automatic value, complete the following actions.

How do I change the default locale in angular for French?

This step is required if you want Angular pipes and directives to support default formats from other locales. Angular uses en-US (English for the United States) as a default locale. Here I’m changing the configuration at the top of app.module.ts to support the locale for the French language in France, which is fr-FR: 2. Set the current locale

What is localization in Angular 7?

Localization is the process for translating the app to a particular language. We need to apply internationalization to the application and after that we can localize it. Localization allows us to serve our application in different languages. The first step is to create an Angular 7 app.

How do I add Spanish language in angular?

Open angular.json file and add the following configuration. Here we have added the configuration for the Spanish language. We have provided the path and format for i18n file and set the locale to “es”. When we execute the application, the app’s content will be served from the i18n file path provided.


1 Answers

Well, not sure it's a good solution but here's what i've done. It works for my purpose so maybe it can help someone else.

in main.ts :

if (localStorage.getItem('locale') === null) {
localStorage.setItem('locale', 'en');
}

const locale = localStorage.getItem('locale');
declare const require;
const translations = require(`raw-loader!./locale/messages.${locale}.xlf`);

platformBrowserDynamic().bootstrapModule(AppModule, {
  providers: [
    {provide: TRANSLATIONS, useValue: translations},
    {provide: TRANSLATIONS_FORMAT, useValue: 'xlf'}
  ]
});

in html code :

<a  mat-menu-item href="" (click)="changeLang('fr')">
    <mat-icon>settings</mat-icon>
    <span>FR</span>
  </a>

  <a  mat-menu-item href="" (click)="changeLang('en')">
    <mat-icon>settings</mat-icon>
    <span>EN</span>
  </a>

in component.ts :

changeLang(lang: string) {

    if (lang === 'fr') {
      localStorage.setItem('locale', 'fr');
    }

    if (lang === 'en') {
      localStorage.setItem('locale', 'en');
    }
  }

don't yell at me, i'm just a newbie with angular ^^

like image 119
j0w Avatar answered Oct 05 '22 23:10

j0w