@planet/localizer v1.4.0
localizer
A localization utility for fetching and formatting ICU messages.
Installation
Add the package as a dependency with npm:
npm install @planet/localizer --saveUse
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-stringRequired 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}.jsonwould expand tohttp://example.com/en.jsonfor theenlocale. Resource files are JSON objects with keys for ICU formatted messages.config.supported-ObjectRequired lookup of supported locales.config.default-stringThe default locale identifier. The resource file for this locale will be loaded at construction. If not provided, the first key in thesupportedlookup will be used as the default.
Methods
update(locale)-function(string)Update the current locale. The requested locale will be resolved to one of thesupportedlocales (e.g. ifen-AUis requested and onlyenis supported,enwill be used). Calling update triggers a fetch for a resource file. When the resource file is loaded (or if it has been previously cached), theupdateevent will be triggered.isRTL(locale)-booleanUtility method for determining if a locale is a right-to-left language.
Properties
current-stringThe currently loaded locale (read only). You should access this property in anupdateevent listener to determine the resolved locale.supported-ObjectThe 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 alocalizefunction for formatting messages for the current locale. Thelocalizefunction takes astringmessage key and an optional lookupObjectof values for replacement in the message.error- Triggered when there is an error loading or parsing a resource file. Listeners will be called with anErrordescribing what went wrong.
Development
Install dependencies and run tests continuously during development:
npm install && npm startTests 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.