Angular 5-日期-语言 [英] Angular 5 - date - language
问题描述
我在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
根据语言环境规则设置日期格式.
Formats a date according to locale rules.
必须像这样使用管道
{{ 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
locale
是一个字符串,用于定义要使用的语言环境(使用当前的 LOCALE_ID 默认)
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{}
您的初始表达式{{viewDate | date:'MMM'}}
现在应该输出德语本地化的缩写月份.
Your initial expression {{viewDate | date:'MMM'}}
should now output the german localized abbreviated month.
这篇关于Angular 5-日期-语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!