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

@planet/localizer

v1.4.0

Published

A localization utility for fetching and formatting ICU messages

Downloads

7

Readme

localizer

A localization utility for fetching and formatting ICU messages.

Installation

Add the package as a dependency with npm:

npm install @planet/localizer --save

Use

Configure your localizer with a URL template and a lookup of supported locales:

var Localizer = require('@planet/localizer');
var localizer = new Localizer({
  url: 'http://example.com/{locale}.json',
  supported: {en: 'English', es: 'Español'},
  default: 'en'
});

This provides you with a localizer that will load resource files for English (en) and Spanish (es) locales.

For this example, assume that http://example.com/en.json provides English messages for your application and has the following content:

{
  "greeting": "Hello {name}!"
}

The localizer is an event emitter that will emit update events when resource files have been loaded and parsed.

localizer.on('update', function(localize) {
  var message = localize('greeting', {name: 'Planet'});
  console.log(message); // "Hello Planet!"
});

The first of the supported locales is the default, and will be loaded after a localizer is constructed. To change locales, call the localizer.update(locale) method.

localizer.update('es');

Let's assume that http://example.com/es.json provides Spanish messages and has the following content:

{
  "greeting": "¡Hola {name}!"
}

Your update listener will now get called with a localize method for formatting Spanish messages:

localizer.on('update', function(localize) {
  var message = localize('greeting', {name: 'Planet'});
  console.log(message); // "¡Hola Planet!"
});

See the Format.js guide for details on the supported ICU message syntax.

API

new Localizer(config)

Arguments

  • config.url - string Required URL template for loading resource files. The template must have a single {locale} placeholder that will be replaced with the current locale identifier. For example, http://example.com/{locale}.json would expand to http://example.com/en.json for the en locale. Resource files are JSON objects with keys for ICU formatted messages.

  • config.supported - Object Required lookup of supported locales.

  • config.default - string The default locale identifier. The resource file for this locale will be loaded at construction. If not provided, the first key in the supported lookup will be used as the default.

Methods

  • update(locale) - function(string) Update the current locale. The requested locale will be resolved to one of the supported locales (e.g. if en-AU is requested and only en is supported, en will be used). Calling update triggers a fetch for a resource file. When the resource file is loaded (or if it has been previously cached), the update event will be triggered.

  • isRTL(locale) - boolean Utility method for determining if a locale is a right-to-left language.

Properties

  • current - string The currently loaded locale (read only). You should access this property in an update event listener to determine the resolved locale.

  • supported - Object The lookup of supported locales provided to the constructor (read only).

Events

  • update - Triggered when resource files are loaded and parsed. Listeners will be called with a localize function for formatting messages for the current locale. The localize function takes a string message key and an optional lookup Object of values for replacement in the message.

  • error - Triggered when there is an error loading or parsing a resource file. Listeners will be called with an Error describing what went wrong.

Development

Install dependencies and run tests continuously during development:

npm install && npm start

Tests may be run a single time with npm test.

License

© Planet Labs, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.