Angular 5 - date - language

时间秒杀一切 提交于 2019-12-10 03:22:25

问题


I use this expression in my Angular 5 application:

{{ viewDate | date:'MMM' }}

The month abbreviation is shown in English. How do I switch the output to German?

[SOLVED] https://angular.io/api/core/LOCALE_ID


回答1:


As you pointed in your edit out already you have to define a locale within your app. The documentation for the DatePipe states

Formats a date according to locale rules.

The pipe has to be used like so

{{ date_expression | date[:format[:timezone[:locale]]] }}

As you can see, the pipe accepts a format, timezone and locale parameter (besides the actual date that is to be parsed). Reading further, documentation states

locale is a string defining the locale to use (uses the current LOCALE_ID by default)

Here's a read up on how the LOCALE definition works. You probably want to localize your entire application in German. First, you want to import the German locales within your AppModule.

import { registerLocaleData } from '@angular/common';
import localeDe from '@angular/common/locales/de';

registerLocaleData(localeDe);

Now you can use the locale as usual

@NgModule({
  // ...
  providers: [{provide: LOCALE_ID, useValue: 'de'}]
})
export class AppModule{}

Your initial expression {{viewDate | date:'MMM'}} should now output the german localized abbreviated month.



来源:https://stackoverflow.com/questions/49850214/angular-5-date-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!