设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

介绍JavaScript国际化API

2014-12-17 11:02| 发布者: joejoe0332| 查看: 3215| 评论: 0|原作者: douxingxiang, 开源中国七里香, daxiang|来自: oschina

摘要: Firefox 29 是在半年之前发布的,所以这篇文章有点过时了。这里我先停一会,讨论一下该桌面版附带的国际化API(已经通过所有得测试)。 大多数的实现是由Norbert Lindenberg 所写,由我审查和维护。(不久Makoto Kat ...


举例

  创建DateTimeFormat对象后,下一步是通过方便的format()函数来格式化date。更方便的是,这个函数是有界函数(bound function):你不必在DateTimeFormat上直接调用。之后给它传递一个时间戳或者Date对象。


  总结一下,下文是如何为特定用途创建DateTimeFormat选项的例子(在当前Firefox执行行为下)。

1
2
3
4
var msPerDay = 24 * 60 * 60 * 1000; 
 
// July 17, 2014 00:00:00 UTC.
var july172014 = new Date(msPerDay * (44 * 365 + 11 + 197));


  我们来格式化美国英语的date。我们先创建一个2位数字的month/day/year, 加上2位数字的hours/minutes, 还有一个短时区来确定这个时间。(结果肯定因不同时区而明显不同)

1
2
3
4
5
6
7
8
var options =
  { year: "2-digit", month: "2-digit", day: "2-digit",
    hour: "2-digit", minute: "2-digit",
    timeZoneName: "short" };
var americanDateTime =
  new Intl.DateTimeFormat("en-US", options).format; 
 
print(americanDateTime(july172014)); // 07/16/14, 5:00 PM PDT


  或者类似的,对葡萄牙语,最好是巴西使用的葡语,但是用在葡萄牙作品中的。格式会稍长,因为包含完整的year和正式拼写的month,但是因移植性要转成UTC格式。

1
2
3
4
5
6
7
8
9
var options =
  { year: "numeric", month: "long", day: "numeric",
    hour: "2-digit", minute: "2-digit",
    timeZoneName: "short", timeZone: "UTC" };
var portugueseTime =
  new Intl.DateTimeFormat(["pt-BR""pt-PT"], options); 
 
// 17 de julho de 2014 00:00 GMT
print(portugueseTime.format(july172014));


  那对于一个压缩的,UTC格式的瑞士火车每周调度表?我们尝试用正式语言按流行度从大到小来选择最易读的一个。

1
2
3
4
5
6
7
8
var swissLocales = ["de-CH""fr-CH""it-CH""rm-CH"];var options =
  { weekday: "short",
    hour: "numeric", minute: "numeric",
    timeZone: "UTC", timeZoneName: "short" };
var swissTime =
  new Intl.DateTimeFormat(swissLocales, options).format; 
 
print(swissTime(july172014)); // Do. 00:00 GMT


  或者我们尝试某个日本博物馆里的一幅画中的描述性文本中的date,这个date使用日本的year和era历法。

1
2
3
4
5
var jpYearEra =
  new Intl.DateTimeFormat("ja-JP-u-ca-japanese",
                          { year: "numeric", era: "long" }); 
 
print(jpYearEra.format(july172014)); // 平成26年


  对一些完全不同的、更长的date,用于泰国泰语,但是使用泰语数字系统和中国历法。(类似Firefox的高质量实现通常会将普通的th-TH当做th-TH-u-ca-buddhist-nu-latn, 因为泰国使用佛历系统和拉丁0-9数字)。

1
2
3
4
5
6
var options =
  { year: "numeric", month: "long", day: "numeric" };
var thaiDate =
  new Intl.DateTimeFormat("th-TH-u-nu-thai-ca-chinese", options); 
 
print(thaiDate.format(july172014)); // ๒๐ 6 ๓๑


  撇开历法和数字系统,还是很简单的。只要选取自己的组件和长度。



酷毙

雷人

鲜花

鸡蛋

漂亮
  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部