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

查看:41
本文介绍了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)

这里阅读了LOCALE的定义作品.您可能希望将整个应用程序本地化为德语.首先,您要在 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天全站免登陆