Each language, and the countries that speak that language, have different expectations when it comes to how numbers (including currency and percentages) and dates should appear. Obviously, each language has different names for the days of the week and the months of the year. But they also have different expectations for the structure of dates, such as what order the day, month and year are in. In number formatting, not only does the character used to delineate number groupings and the decimal portion differ, but the placement of those characters differ as well.
A user using an application should be able to read and write dates and numbers in the format they are accustomed to. This library makes this possible, providing an API to convert user-entered number and date strings - in their own format - into actual numbers and dates, and conversely, to format numbers and dates into that string format.
Even if the application deals only with the English locale, it may still need globalization to format programming language bytes into human-understandable language and vice-versa in an effective and reasonable way. For example, to display something better than "Edited 1 minutes ago".
Globalize provides number formatting and parsing, date and time formatting and parsing, currency formatting, message formatting (ICU message format pattern), and plural support.
Globalize is based on the Unicode Consortium's Common Locale Data Repository (CLDR), the largest and most extensive standard repository of locale data available. CLDR is constantly updated and is used by many large applications and operating systems, so you'll always have access to the most accurate and up-to-date locale data.
Globalize needs CLDR content to function properly, although it doesn't embed, hard-code, or host such content. Instead, Globalize empowers developers to load CLDR data the way they want. Vanilla CLDR in its official JSON format (no pre-processing) is expected to be provided. As a consequence, (a) Globalize avoids bugs caused by outdated i18n content. Developers can use up-to-date CLDR data directly from Unicode as soon as it's released, without having to wait for any pipeline on our side. (b) Developers have full control over which locale coverage they want to provide on their applications. (c) Developers are able to share the same i18n dataset between Globalize and other libraries that leverage CLDR. There's no need for duplicating data.
Globalize is systematically tested against desktop and mobile browsers and Node.js. So, using it you'll get consistent results across different browsers and across browsers and the server.
Are you coming from Globalize 0.x? Read our migration guide to learn what have changed and how to migrate older 0.x code to up-to-date 1.x.
Globalize uses the Unicode CLDR, the largest and most extensive standard repository of locale data.
We do NOT embed any i18n data within our library. However, we make it really easy to use. Read How to get and load CLDR JSON data for more information on its usage.
|File||Minified + gzipped size||Summary|
|globalize/currency.js||+2.6KB||Currency module provides currency formatting and parsing|
|globalize/date.js||+4.9KB||Date module provides date formatting and parsing|
|globalize/message.js||+5.4KB||Message module provides ICU message format support|
|globalize/number.js||+2.9KB||Number module provides number formatting and parsing|
|globalize/plural.js||+1.7KB||Plural module provides pluralization support|
|globalize/relative-time.js||+0.7KB||Relative time module provides relative time formatting support|
Globalize 1.x supports the following browsers:
(Current - 1) or Current denotes that we support the current stable version of the browser and the version that preceded it. For example, if the current version of a browser is 24.x, we support the 24.x and 23.x versions.
You need to satisfy Globalize dependencies prior to using it. The good news is, there is only one. It's the cldr.js, which is a CLDR low level manipulation tool.
If you use a package manager like bower or npm, you don't need to worry about it. If this isn't the case, then you need to manually download cldr.js yourself. Check the Hello World examples for more information.
Globalize is the i18n software (the engine). Unicode CLDR is the i18n content (the fuel). You need to feed Globalize on the appropriate portions of CLDR prior to using it.
(a) How do I figure out which CLDR portions are appropriate for my needs?
Each Globalize function requires a special set of CLDR portions. Once you know which Globalize functionalities you need, you can deduce its respective CLDR requirements. See table below.
|Module||Required CLDR JSON files|
+CLDR JSON files from number module
+CLDR JSON files from plural module for name style support
+CLDR JSON files from number module
|Plural module||cldr/supplemental/plurals.json (for cardinals)|
cldr/supplemental/ordinals.json (for ordinals)
|Relative time module||cldr/main/|
+CLDR JSON files from number and plural modules
(b) How am I supposed to get and load CLDR content?
By downloading a ZIP or a TAR.GZ...
Click the github releases tab and download the latest available Globalize package.
By using a package manager...
bower install globalize, or npm
npm install globalize cldr-data.
By using source files...
git clone https://github.com/jquery/globalize.git.
Globalize's consumable-files are located in the
./dist directory. If you
don't find it, it's because you are using a development branch. You should
either use a tagged version or build the distribution files yourself.
Read installation above if you need more information on how to
Globalize can be used for a variety of different i18n tasks, eg. formatting or
parsing dates, formatting or parsing numbers, formatting messages, etc. You may
NOT need Globalize in its entirety. For that reason, we made it modular. So, you
can cherry-pick the pieces you need, eg. load
dist/globalize.js to get
Globalize core, load
dist/globalize/date.js to extend Globalize with Date
An example is worth a thousand words. Check out our Hello World demo (available to you in different flavors):
Globalize.load( cldrJSONData, ... )
This method allows you to load CLDR JSON locale data.
Globalize.load() is a
Globalize.locale( [locale|cldr] )
Set default locale, or get it if locale argument is omitted.
[new] Globalize( locale|cldr )
Create a Globalize instance.
.dateFormatter( pattern )
Return a function that formats a date according to the given
dateFormatter "GyMMMd"// > "Nov 30, 2010 AD"dateFormatter date: "medium"// > "Nov 1, 2010"dateFormatter time: "medium"// > "5:55:00 PM"dateFormatter datetime: "medium"// > "Nov 1, 2010, 5:55:00 PM"
.dateParser( pattern )
Return a function that parses a string date according to the given
dateParser "GyMMMd" "Nov 30, 2010 AD"// > new Date()dateParser date: "medium" "Nov 1, 2010"// > new Date()dateParser time: "medium" "5:55:00 PM"// > new Date()dateParser datetime: "medium" "Nov 1, 2010, 5:55:00 PM"// > new Date()
.formatDate( value, pattern )
.dateFormatter( pattern )( value ).
.parseDate( value, pattern )
.dateParser( pattern )( value ).
Globalize.loadMessages( json )
Load messages data.
.messageFormatter( path ) ➡ function([ variables ])
Return a function that formats a message (using ICU message format pattern) given its path and a set of variables into a user-readable string. It supports pluralization and gender inflections.
messageFormatter "task" 1000// > "You have 1,000 tasks remaining"messageFormatter "like" 3// > "You and 2 others liked this"
.formatMessage( path [, variables ] )
.messageFormatter( path )([ variables ]).
.numberFormatter( [options] )
Return a function that formats a number according to the given options or locale's defaults.
numberFormatter pi// > "3.142"numberFormatter maximumFractionDigits: 5 pi// > "3.14159"numberFormatter round: "floor" pi// > "3.141"numberFormatter minimumFractionDigits: 2 10000// > "10,000.00"numberFormatter style: "percent" 0.5// > "50%"
.numberParser( [options] )
Return a function that parses a string representing a number according to the given options or locale's defaults.
numberParser "3.14159"// > 3.14159numberParser "10,000.00"// > 10000numberParser style: "percent" "50%"// > 0.5
.formatNumber( value [, options] )
.numberFormatter( [options] )( value ).
.parseNumber( value [, options] )
.numberParser( [options] )( value ).
.currencyFormatter( currency [, options] )
Return a function that formats a currency according to the given options or locale's defaults.
currencyFormatter "USD" 1// > "$1.00"currencyFormatter "USD" style: "accounting" -1// > "($1.00)"currencyFormatter "USD" style: "name" 69900// > "69,900.00 US dollars"currencyFormatter "USD" style: "code" 69900// > "69,900.00 USD"currencyFormatter "USD" round: "ceil" 1.491// > "$1.50"
.formatCurrency( value, currency [, options] )
.currencyFormatter( currency [, options] )( value ).
.pluralGenerator( [options] )
Return a function that returns the value's corresponding plural group:
The function may be used for cardinals or ordinals.
pluralGenerator 0// > "other"pluralGenerator 1// > "one"pluralGenerator type: "ordinal" 1// > "one"pluralGenerator type: "ordinal" 2// > "two"
.plural( value[, options ] )
.pluralGenerator( [options] )( value ).
.relativeTimeFormatter( unit [, options] )
Returns a function that formats a relative time according to the given unit, options, and the default/instance locale.
relativeTimeFormatter "day" 1// > "tomorrow"relativeTimeFormatter "month" -1// > "last month"relativeTimeFormatter "month" 3// > "in 3 months"
.formatRelativeTime( value, unit [, options] )
.relativeTimeFormatter( unit, options )( value ).
Thrown when a CLDR item has an invalid or unexpected value.
Thrown when any required CLDR item is NOT found.
Thrown when a parameter has an invalid type on any static or instance methods.
Thrown for certain parameters when the type is correct, but the value is invalid.
Thrown when a required parameter is missing on any static or instance methods.
Thrown when a parameter is not within a valid range of values.
Thrown when any static method, eg.
Globalize.formatNumber() is used prior to
setting the Global locale with
Globalize.locale( <locale> ).
Thrown when plural module is needed, but not loaded, eg. to format currencies using the named form.
Thrown for unsupported features, eg. to format unsupported date patterns.
The source files are as granular as possible. When combined to generate the build file, all the excessive/overhead wrappers are cut off. It's following the same build model of jQuery and Modernizr.
Core, and all modules' public APIs are located in the
src/ directory, ie.
npm install && bower install
Tests can be run either in the browser or using Node.js (via Grunt) after having installed the external development dependencies (for more details, see above).
To run the unit tests, run
grunt test:unit, or run
http://localhost:9001/test/unit.html in a browser (or
http://localhost:9001/test/unit-es5-shim.html for IE8). It tests the very
specific functionality of each function (sometimes internal/private).
The goal of the unit tests is to make it easy to spot bugs, easy to debug.
To run the functional tests, create the dist files by running
grunt. Then, run
grunt test:functional, or open
a browser (or
http://localhost:9001/test/functional-es5-shim.html for IE8).
grunt will automatically run unit and functional tests for you to
ensure the built files are safe.
The goal of the functional tests is to ensure that everything works as expected when it is combined.
Build the distribution files after having installed the external development dependencies (for more details, see above).