AngularJS翻译:格式动态日期 [英] AngularJS translate: Format dynamic dates

查看:212
本文介绍了AngularJS翻译:格式动态日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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