Angular 5-日期-语言 [英] Angular 5 - date - language

查看:87
本文介绍了Angular 5-日期-语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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]]] }}

如您所见,管道接受formattimezonelocale参数(除了要解析的实际日期). 进一步阅读文档说明

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)

此处是. 您可能希望将整个应用程序本地化为德语. 首先,您要在AppModule中导入德语语言环境.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆