问题描述
我在Angular 5应用程序中使用此表达式:
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?
[已解决] https://angular.io/api/core/LOCALE_ID
推荐答案
正如您在编辑中指出的那样,您必须在应用程序中定义语言环境. DatePipe 状态的文档
As you pointed in your edit out already you have to define a locale within your app. The documentation for the DatePipe states
必须像这样使用管道
{{ date_expression | date[:format[:timezone[:locale]]] }}
如您所见,管道接受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
此处是.您可能希望将整个应用程序本地化为德语.首先,您要在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{}
您的初始表达式{{viewDate | date:'MMM'}}
现在应该输出德语本地化的缩写月份.
这篇关于Angular 5-日期-语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!