Skip to content
/ ecma402 Public

ECMA-402 JavaScript Internationalization API "shim"

License

Notifications You must be signed in to change notification settings

ibm-js/ecma402

Repository files navigation

ecma402 Build Status

This project provides an implementation of the ECMA-402 JavaScript Internationalization APIs standard for number formatting ( Intl.NumberFormat ) and date and time formatting ( Intl.DateTimeFormat ). Collation ( Intl.Collator ) is not currently supported.

Status

Current release is version 1.0, published 2015-07-08.

Licensing

This project is distributed by the Dojo Foundation and licensed under the "New" BSD License. All contributions require a Dojo Foundation CLA.

Dependencies

This project requires the following other projects to run:

Installation

Bower release installation:

$ bower install ecma402

Manual master installation:

$ git clone git://github.com/ibm-js/ecma402.git

Then install dependencies with bower (or manually from github if you prefer to):

$ cd ecma402
$ bower install

Documentation

Once installed, you should first include RequireJS AMD loader in your appication:

<script src="bower_components/requirejs/require.js"></script>

Then you can load Intl through the ecma402/IntlShim module and use it:

<script>
   // configuring RequireJS
   require.config({
     // where to find ecma402 package
     baseUrl: "bower_components",
     // which locales do I want to be available in addition to default browser locale
     config: {
        "ecma402/locales": /^(en|de|fr)$/
     }
   });
</script>
<script>
  require(["ecma402/IntlShim"], function(Intl) {
     var nf = Intl.NumberFormat("fr", { style: "percent" });
     console.log(nf.format(24.02));
  });
</script>

Note that by default IntlShim will use native implementation if available in the browser, if not available it will load its own JavaScript CLDR-based implementation. In order to force the JavaScript implementation you can force the intl-api flag to false in the RequireJS config as follows:

<script>
   // configuring RequireJS
   require.config({
     // where to find ecma402 package
     baseUrl: "bower_components",
     config: {
       "requirejs-dplugins/has": {
       	  "intl-api": false
       }
     }
   });
</script>

Alternatively, if you always want to use the Intl as provided through this package, and not attempt to leverage the native support in the browser, you can load the ecma402/Intl module directly, as follows:

<script>
   // configuring RequireJS
   require.config({
     // where to find ecma402 package
     baseUrl: "bower_components",
     // which locales do I want to be available in addition to default browser locale
     config: {
        "ecma402/locales": /^(en|de|fr)$/
     }
   });
</script>
<script>
  require(["ecma402/Intl"], function(Intl) {
     var nf = Intl.NumberFormat("fr", { style: "percent" });
     console.log(nf.format(24.02));
  });
</script>

For further documentation on the ECMA-402 standard Intl API and how to use it you can check out the information available at MDN

Locale configuration using ecma402/locales

This package contains locale data for a large number of locales as defined by Unicode CLDR For many run time environments, it is not necessary to load the data for all possible locales, but instead, only for the default locale as defined by the settings in the browser. In order to keep the data load time to a minimum, by default only data associated with the default locale is loaded at run time unless additional locales are specified using the config option "ecma402/locales" as in the above example. The argument can be either a regular expression, a string (for a single locale), or an array of strings.