D3 时间格式化语言环境

D3 time formatting locale

是否可以从 D3 时间格式 return 本地化信息?

d3.time.format('%B %Y')(new Date(d))

这总是 return EN 值。

是的,这是可能的。有一个 github wiki 页面 here

基本上你需要使用d3.locale(definition)方法。定义是这样一个对象:

{
  "decimal": ".",
  "thousands": ",",
  "grouping": [3],
  "currency": ["$", ""],
  "dateTime": "%a %b %e %X %Y",
  "date": "%m/%d/%Y",
  "time": "%H:%M:%S",
  "periods": ["AM", "PM"],
  "days": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
  "shortDays": ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
  "months": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  "shortMonths": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
}

更新

The formatting of numbers, dates and currencies varies by language and locale. While the default build of D3 is intended for U.S. English, you can change the behavior of D3’s formatters by loading new locales as needed.

因此,语言环境不会自动识别,您应该自行加载。

小例子:

var ua = d3.locale(<uk_UA definition>); //you need this object to be available everywhere this locale is required
ua.timeFormat('%B %Y'); //use this instead of d3.time.format('%B %Y')