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

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

问题描述

使用AngularJS和angular-translate我试图在翻译的文本中插入一个日期作为参数。



翻译包中记录了基本任务: p>

 < p> {{'MyText'|翻译:{myDate:someControllerDate}}}< / p> 

在本地化的json文件中:



$ {pre> (英文)'MyText':'这是日期:{{myDate}}'
(丹麦语)'MyText':'Dette {{myDate}}呃datoen'

这给了我:



(英文)这是日期:2015-04-29T00:00:00。



(丹麦语)Dette 2015 -04-29T00:00:00 er datoen。



问题:
我想格式化日期匹配语言(或文化,但现在语言将是足够好的)。



所需的结果是:



(英文)这是日期:04-29-



(丹麦语)Dette 29-04-2015 er datoen。



我希望按照以下一行语法:

 (英文)'MyText':'这是日期:{{myDate:MM-dd-yyyy}}'
(丹麦语)'MyText':Dette {{myDate:dd-MM-yyyy}}呃datoen'

或者也许:

  p> {{'MyText'|翻译:{{myDate:someControllerDate |翻译:'MyDateFormat'}}}}< / p> 

 (英文)'MyDateFormat':'MM-dd-yyyy'
(丹麦语)'MyDateFormat':'dd-MM-yyyy'

有没有办法实现所需的结果,最好不必格式化控制器内的日期(保持逻辑和视图分开)?

解决方案

假设您有以下 angular-translate 翻译定义:

  // de 
with_date:German:{{date | date:'short'}}

/ / en
with_date:英文:{{date | date:'medium'}}

然后在视图中可以执行以下操作:

 < h1> {{'with_date'| translate:{date :今天}}}< / h1> 

其中今天在控制器中定义ie:

  $ scope.today = new Date(); 

假设您加载了 角度区域设置_ * 具有正确的区域设置,日期将以语言/国家/地区的方式进行格式化。



这是一个演示



ngLocale )非常有限,当谈到日期时,您可以通过 moment.js 完成奇迹


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>

with this in a localized json-file:

(english)'MyText': 'This is the date: {{myDate}}.'
(danish) 'MyText': 'Dette {{myDate}} er datoen.'

This gives me:

(english) This is the date: 2015-04-29T00:00:00.

(danish) Dette 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).

The desired result is:

(english) This is the date: 04-29-2015.

(danish) Dette 29-04-2015 er 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.'

Or perhaps:

<p>{{ 'MyText' | translate:{{myDate:someControllerDate | translate:'MyDateFormat'}} }}</p>

with

(english)'MyDateFormat': 'MM-dd-yyyy'
(danish) 'MyDateFormat': 'dd-MM-yyyy'

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>

Where today is defined in controller i.e.:

$scope.today = new Date();

Assuming you've loaded angular-locale_* with correct locale the dates will be formatted in a language/country specific way.

Here's a demo.

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天全站免登陆