I noticed that dates are not properly formatted according to the browser language in my angular application when using the date
pipe. To fix this, I tried to implement the suggestion from this answer in my app.module.ts
. As I am using ngx-translate
in my application, I used the TranslateService
to get the current language like this:
{
provide: LOCALE_ID,
useFactory: (translate: TranslateService) => {
console.log("Current language", translate.currentLang);
return translate.currentLang;
},
deps: [TranslateService],
},
Unfortunately, this doesn't work. translate.currentLang
seems to still be set to undefined
here so I cannot use it the way I wanted. I assume this is because this code gets executed before I even set the preferred language in the constructor of my app.component.ts:
constructor(
public translate: TranslateService,
) {
translate.addLangs(["en", "de"]);
const browserLang = translate.getBrowserLang();
translate.use(browserLang.match(/en|de/) ? browserLang : "en");
}
Is there a way to make this work so I don't need to come up with some sort of workaround to get the current language in my app.module.ts
?