Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the value of --locale on runtime for an AOT compiled Angular App

I have an multilanguale app which is compiled with --aot in each language, for instance for German:

ng build --aot --env=prod --prod --build-optimizer --i18nFile=src/locale/DARI_messages_de_DE.xlf --i18nFormat=xlf --locale=de --missingTranslation warning --output-path dist/de --base-href /de/

We need want to get the value on locale on runtime to handle it over to our backend too.

If have looked tryed to get from

    import { LOCALE_ID } from '@angular/core';

constructor(private modalService: BsModalService) {
  console.log("LOCALE_ID", LOCALE_ID);
}

But the LOCAL_ID is empty I think it only for use with JIT

Is there any method to get this parameter on runtime?

like image 354
Thomas Avatar asked Jun 14 '18 16:06

Thomas


People also ask

What is locale in angular?

Angular uses the Unicode locale identifier (Unicode locale ID) to find the correct locale data for internationalization of text strings. Unicode locale ID. A locale ID conforms to the Unicode Common Locale Data Repository (CLDR) core specification.

Where do we set the default language in angular?

By default, Angular uses the locale en-US . To set your application locale to another value use the CLI parameter locale with the proper value that you want to use (note that angular follows the Unicode LDML convention). For example, the command above would serve your application setting the locale to Spanish.


2 Answers

As a newbie I found Simons answer a bit incomplete. Turns out that you need to import both LOCALE_ID and Inject:

import { Component, OnInit, LOCALE_ID, Inject } from '@angular/core';

@Component({
  selector: '...',
  templateUrl: '...',
  styleUrls: ['...']
})

export class ShinyComponent implements OnInit {

  constructor(@Inject(LOCALE_ID) protected localeId: string) { }

  public someFunction {} {
    console.log("Current locale is "+this.localeId); 
  }
}

ngOnInit() {}
like image 146
Jette Avatar answered Oct 08 '22 20:10

Jette


You should have Angular inject the LOCALE_ID

constructor(private modalService: BsModalService,
    @Inject( LOCALE_ID ) protected localeId: string) {
    console.log("LOCALE_ID", localeId);
}
like image 5
Simon Avatar answered Oct 08 '22 21:10

Simon