How to access culture data in globalize.js V1.0.0

2019-07-11 19:10发布

问题:

I am migrating from Globalize.js V0.0.1 to V1.0.0. In V0.0.1 it was possible to access the loaded culture data as you can see below. How can I access the data with current version 1.0.0.

var culture = Globalize.culture("en-US");
culture.calendar.months.names; // returns: ["January", "February", "March", ...
culture.calendar.days.names;  // returns: ["Sunday", "Monday", "Tuesday", ... 

Thank you very much in advance!

回答1:

In Globalize 0.x, the i18n content was mixed/embedded into the library. Now in Globalize 1.x, we use external CLDR.

CLDR can be accessed via https://github.com/unicode-cldr/, or you can get it installed locally with:

$ npm install cldr-data

Globalize, under the hoods, traverse CLDR data using Cldrjs. You can use it yourself independently of Globalize to traverse CLDR data, for example:

$ npm install cldr-data cldrjs
$ node
> var Cldr = require("cldrjs");
> Cldr.load(require("cldr-data").entireSupplemental());
> Cldr.load(require("cldr-data").entireMainFor("en"));
> 
> var en = new Cldr("en");
> en.main("dates/calendars/gregorian/months/format/wide/1");
'January'
>
> // Note the "{region}" fragment is automatically substituted by the instance's
> // region subtag. See `en.attributes` for all of those variables.
> en.supplemental("currencyData/region/{region}");
[ { USN: { _tender: 'false' } },
  { USS: { _to: '2014-03-01', _tender: 'false' } },
  { USD: { _from: '1792-01-01' } } ]

If you are already using Globalize, you can access this data using its own Cldrjs instance (for convenience):

$ npm install globalize cldr-data
$ node
> var Globalize = require("Globalize");
> Globalize.load(require("cldr-data").entireSupplemental());
> Globalize.load(require("cldr-data").entireMainFor("en"));
> 
> var en = new Globalize("en");
> en.cldr.main("dates/calendars/gregorian/months/format/wide/1");
'January'

More info at https://github.com/rxaviers/cldrjs and https://github.com/jquery/globalize

Just let me know on any question.