Search code examples
angularinternationalization

Angular: How to get the current locale at runtime when using AOT


I am compiling my project with:

ng serve --aot --i18nFile=client/locale/messages.fr.xlf --i18nFormat=xlf --locale=fr

How can I access the locale ID at runtime? I want to show/hide elements based on the locale.

PS. I do realise that using JIT compilation I can simply do this:

providers: [ { provide: LOCALE_ID, useValue: 'fr' } ]

But I'm looking for a AOT solution. I also would rather not infer locale based on hostname or anything like that.


Solution

  • Simply inject LOCALE_ID in your constructor, e.g.

    import { LOCALE_ID, Inject } from '@angular/core';
    
    ...
    
    constructor(
      @Inject(LOCALE_ID) public locale: string
    ) { }