npm

Bring the best of OSS JavaScript development to your projects with npm Orgs - private packages & team management tools.Learn more »

localizable-intl

1.2.8 • Public • Published

LocalizableIntl.js

I once got a little in trouble with localizing my NodeJS-module properly since NodeJS has no locales but the English one.
Sadly I'm not an English user and I want NodeJS to print dates and numbers in the format according to my current culture (language and region).

For that reason I started to look for some way to get NodeJS' globalization to work.

After some days or hours or... actually I don't really remember... I stumbled over Intl.js.
Intl.js generally adds some classes to the Intl-namespaces which will make you able to format dates or numbers properly, for instance:

console.log(new Intl.DateTimeFormat('de-CH', { month: long }).format(new Date()))

Which prints the current month in German.
Sadly the normal toLocaleString-calls are still performed in English.

That's why I came to the decision to fork and extend this module.

Installation

You can install this module using npm:

npm install --save localizable-intl

Usage

At first you need to patch the NodeJS-runtime using Intl polyfill:

import * as IntlPolyfill from 'localizable-intl';
 
if (global.Intl) {
    // polyfill and patch the constructors we need with the polyfill's.
    Intl.NumberFormat   = IntlPolyfill.NumberFormat;
    Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
} else {
    // No `Intl`, so use and load the polyfill. 
    global.Intl = IntlPolyfill;
}

You may want to set the default locale after this using the __setDefaultLocale-method:

(Intl as any).__setDefaultLocale('de-CH');

After this you're done!
NodeJS' globalization will work like a dream now!
You can try it out using this example:

new Date(9E8).toLocaleString();
// This prints the correclty localized date
// rather than the date in English.
//
// Output with Polyfilled Intl and default-locale set to 'de-CH':
// 11.1.1970, 11:00:00
//
// Output without Pollyfilled Intl:
// 1970-1-11 11:00:00

About LocalizableIntl.js

What does it do?

LocalizableIntl.js overwrites the classes DateTimeFormat and NumberFormat with new versions which support globalization. These are provided by Intl.js.

Additionally it overwrites following localizable functions in order to make them using the new DateTimeFormat and NumberFormat:

  • Date.prototype.toLocaleString
  • Date.prototype.toLocaleDateString
  • Date.prototype.toLocaleTimeString
  • Number.prototype.toLocaleString

Troubleshooting

You may run into troubles using localizable functions, for instance when using mocha, causing weird RegEx-errors.

You may want to disable RegEx-cache to make it work properly.
Have a look at Disable RegEx-Cache in order to do so.
Only do this when it's really necessary.

Features

Locale Data

Intl.js uses the Unicode CLDR locale data, as recommended by the specification. The main Intl.js file contains no locale data itself. In browser environments, the data should be provided, passed into a JavaScript object using the Intl.__addLocaleData() method. In Node.js, or when using require('intl'), the data is automatically added to the runtime and does not need to be provided.

Contents of the locale-data directory are a modified form of the Unicode CLDR data found at http://www.unicode.org/cldr/.

Disable RegEx-Cache

Intl.js attempts to cache and restore static RegExp properties before executing any regular expressions in order to comply with ECMA-402. This process is imperfect, and some situations are not supported. This behavior is not strictly necessary, and is only required if the app depends on RegExp static properties not changing (which is highly unlikely). To disable this functionality, invoke Intl.__disableRegExpRestore().

Setting the default locale

Node's behaviour causes all localizable calls to be processed in English.
In order to set the default language, invoke following code-line:

Intl.__setDefaultLocale('de-CH');

License

Copyright (c) 2017 by Manuel Thalmann

This software is licensed under the MIT license. See the LICENSE file accompanying this software for terms of use.

install

npm i localizable-intl

Downloadsweekly downloads

135

version

1.2.8

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability