AngularJS翻译:格式动态日期 [英] AngularJS translate: Format dynamic dates
问题描述
使用AngularJS和角翻译我试图插入一个日期作为参数的翻译文本。
Using AngularJS and angular-translate I am trying to insert a date as parameter in a translated text.
其基本任务是由翻译软件包记载:
The basic task is documented by the translate package:
<p>{{ 'MyText' | translate:{myDate:someControllerDate} }}</p>
这个在本地化的JSON文件:
with this in a localized json-file:
(english)'MyText': 'This is the date: {{myDate}}.'
(danish) 'MyText': 'Dette {{myDate}} er datoen.'
这给了我:
的(英文)这是日期:2015-04-29T00:00:00 的
的(丹麦)德特2015-04-29T00:00:00 ER datoen 的
问题:
我想格式化以匹配该语言的日期(或文化,但是现在的语言就可以了)。
The problem: I would like to format the date to match the language (or culture, but for now the language will be good enough).
期望的结果是:
的(英文)这是日期:2015年4月29日的
的(丹麦)德特29-04-2015呃datoen。的
我希望沿着这些路线语法:
I was hoping for a syntax along these lines:
(english)'MyText': 'This is the date: {{myDate:MM-dd-yyyy}}.'
(danish) 'MyText': Dette {{myDate:dd-MM-yyyy}} er datoen.'
也许
<p>{{ 'MyText' | translate:{{myDate:someControllerDate | translate:'MyDateFormat'}} }}</p>
与
(english)'MyDateFormat': 'MM-dd-yyyy'
(danish) 'MyDateFormat': 'dd-MM-yyyy'
有没有一种方法,以达到预期的效果,preferably无需格式化控制器内部的日期(保持逻辑视图和分离)?
Is there a way to achieve the desired result, preferably without having to format the date inside the controller (keeping logic and view separated)?
推荐答案
假设你有以下角翻译
翻译定义:
Assuming you have following angular-translate
translations definitions:
//de
"with_date": "German: {{date|date:'short'}}"
//en
"with_date": "English: {{date|date:'medium'}}"
然后一个视图中,你可以做:
Then inside a view you can do:
<h1>{{ 'with_date'|translate:{date:today} }}</h1>
其中,今天
在控制器即定义为:
$scope.today = new Date();
假设你已经加载 角区域_ *
,提供正确的区域设置的日期将在语言/国家特定的方式进行格式化。
Assuming you've loaded angular-locale_*
with correct locale the dates will be formatted in a language/country specific way.
下面是一个演示。
说了这么多的本地化机制内置角速度( ngLocale
)是非常有限的,当谈到约会,你可以完成与 moment.js 奇观一>
Having said that localization mechanism built into angular (ngLocale
) is very limited and when it comes to dates you can accomplish wonders with moment.js
这篇关于AngularJS翻译:格式动态日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!