Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 获取AOT编译的应用程序运行时的--locale值_Angular_Internationalization_Angular2 Aot_Angular I18n - Fatal编程技术网

Angular 获取AOT编译的应用程序运行时的--locale值

Angular 获取AOT编译的应用程序运行时的--locale值,angular,internationalization,angular2-aot,angular-i18n,Angular,Internationalization,Angular2 Aot,Angular I18n,我有一个多语言应用程序,每种语言都使用--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/ 我们需要在运行时获取locale上的值,以便

我有一个多语言应用程序,每种语言都使用--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/
我们需要在运行时获取locale上的值,以便将其处理到后端

如果你看起来想从

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

constructor(private modalService: BsModalService) {
  console.log("LOCALE_ID", LOCALE_ID);
}
但是本地ID是空的,我想它只用于JIT


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

您应该具有区域设置ID

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

作为一个新手,我发现西蒙斯的回答有点不完整。事实证明,您需要导入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() {}

你用的是什么角度的?我在Angular 6上,使用--i18n locale=de仍然会产生一个locale\u ID“en-US”。@Simon这是AngularIs的默认位置还有其他方法吗?我想在不使用构造函数的情况下完成它。有可能吗?我还是用同样的方法来做。我还没有发现其他方法。