在运行时为AOT编译的Angular App获取--locale的值

Tho*_*mas 9 internationalization angular-i18n angular2-aot angular

我有一个多语言的应用程序,使用每种语言的--aot编译,例如德语:

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/
Run Code Online (Sandbox Code Playgroud)

我们需要在运行时获取locale的值,以便将它处理到我们的后端.

如果看起来试图得到

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

constructor(private modalService: BsModalService) {
  console.log("LOCALE_ID", LOCALE_ID);
}
Run Code Online (Sandbox Code Playgroud)

但是LOCAL_ID是空的我认为它只适用于JIT

是否有任何方法可以在运行时获取此参数?

Jet*_*tte 16

作为一个新手我发现西蒙斯回答有点不完整.原来你需要导入LOCALE_ID和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() {}
Run Code Online (Sandbox Code Playgroud)


Sim*_*mon 5

你应该让 Angular 注入 LOCALE_ID

constructor(private modalService: BsModalService,
    @Inject( LOCALE_ID ) protected localeId: string) {
    console.log("LOCALE_ID", localeId);
}
Run Code Online (Sandbox Code Playgroud)