Angular:如何以法语格式显示日期

编程入门 行业动态 更新时间:2024-10-24 22:23:23
本文介绍了Angular:如何以法语格式显示日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我是一个 Angular 初学者,我阅读了 Angular 的文档,对于这样一个基本的事情来说很难......我希望我的应用程序中的日期和其他内容具有法语语言环境,而不是默认的en-"美国……

I am an Angular beginner, I read the documentation of Angular, and it's hard for such an elementary thing... I want that the dates and other things in my application have the French locale, and not the default 'en-US'...

我开始阅读这个 Angular 文档,这似乎有点不完整,因为,我做了文档状态,并失败:

I started to read this Angular documentation, that seems a little bit incomplete, cause, I did the doc states, and failed:

>...\ClientApp>ng serve --configuration=fr
Configuration 'fr' could not be found in project 'ClientApp'.

好的,现在我查看日期管道上的另一个文档页面.它指出:

OK, now I look on another documentation page on the Date pipe. It states:

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

但是关于如何使用 locale 的任何示例,因此,我尝试在测试应用程序中执行此操作 link,像这样{{myDate |date: 'medium': undefined : 'fr'}} 但它什么也没显示......我在控制台中有:

but ANY example on how to use the locale, so, I tried to do it in a test application link, like this {{myDate | date: 'medium': undefined : 'fr'}} but it displays nothing... I have in the console:

ERROR
Error: InvalidPipeArgument: 'Missing locale data for the locale "fr".' for pipe 'DatePipe'

我还应该做什么或安装什么才能在 Angular 中以法语格式显示日期?

what else should I do or install to display in Angular a date in the French format?

Angular CLI: 6.1.5
Node: 8.11.1
OS: win32 x64
Angular: 6.1.8

推荐答案

尝试将以下代码添加到您的应用模块

Try adding to your app module the following code

import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';

// the second parameter 'fr' is optional
registerLocaleData(localeFr, 'fr');

https://angular.io/guide/i18n#i18n-pipes

然后,如果您想将此语言环境设置为默认值,则需要将 LOCALE_ID 注入令牌设置为fr",如下所示:

Then if you want to sets this locale as default you need to set the LOCALE_ID injection token as 'fr' like that :

{provide: LOCALE_ID, useValue: 'fr' }

在您的应用模块中

希望能帮到你

这篇关于Angular:如何以法语格式显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 17:08:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1027042.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:法语   日期   格式   Angular

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!