npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

localizable-intl

v1.2.8

Published

Polyfill the ECMA-402 Intl API (except collation)

Downloads

9

Readme

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.