Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@planet/localizer

Package Overview
Dependencies
Maintainers
5
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@planet/localizer

A localization utility for fetching and formatting ICU messages

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
5
Weekly downloads
 
Created
Source

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 list of supported locales:

var Localizer = require('@planet/localizer');
var localizer = new Localizer({
  url: 'http://example.com/{locale}.json',
  supported: ['en', 'es']
});

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 - Array.<string> Required list of supported locales. The first listed locale is the default, and a resource file for this locale will be loaded after construction.

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.

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 - Array.<string> The list 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.

FAQs

Package last updated on 23 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc