l10n-translator

1.1.0 • Public • Published

Translator

A simple module for loading l10n catalogs and retrieving verbiage from them. Work in progress, fits my current usage case better than some of the other modules available. Likely not production ready until I implement the ability to specify a path.

Build Status

Build Status

Installation

If you're a Node user, Translator is available on npm:

$ npm install l10n-translator

If you're interested, you can also:

npm test

or

make test

To verify things are still wired up correctly.

Usage

Using Translator is relatively simple, currently only set up for usage as a require module. Out of the box looks like this:

var Translator = require('l10n-translator'),
    catalog = new Translator('relative/project/path');
 
term = catalog.lookup('PROPERTY_NAME');
// -> { code: 'PROPERTY_NAME', locale: 'en_us', value: null }

You can specify the default/fallback locale by:

var catalog = new Translator('relative/project/path','en_fr');

or by changing it:

catalog.setLocale('en_fr');

or at will:

catalog.lookup('PROPERTY_NAME', req.user.locale)

Catalog Files

Catalog locale files should be placed in the folder you provide when creating a new Translator. They should end in .js, be named simple things like en_us or en_fr, and be formatted something like this:

"locale/en_us.js"
// l10n `en_us`
var defs = module.exports = {};
 
// constants style
defs.SIGNIN_FAILED = "You couldn't sign in. Error Code: #104";
defs.SUBMIT_BUTTON = "Submit";
defs.SIGN_OUT_ACTION = "Log out";

Behavior

Loads all *.js files in a relative directory and attaches them to the translator.catalog. In the future locales will be able to be specified. The default locale is currently en_us.

If you attempt to lookup a value from a catalog (en_fr) that does not exist or is not loaded, Translator will try to return the value from the default catalog.

var term = catalog.lookup('known_value', 'loaded_catalog');
// -> { code: 'known_value', locale: 'loaded_catalog', value: VALUE }
 
var term = catalog.lookup('known_value', 'null_catalog');
// -> { code: 'known_value', locale: 'default_catalog', value: VALUE }

This allows you to decide to ignore the result and pass it through, or strictly enforce only showing it if the locale matches your desired input locale. Value is returned as null when no results were able to be found.

Changelog

1.1.0

  • Terms returned now return term.value when cast as a String (as opposed to [object Object])

1.0.1

  • Updated README
  • Added Makefile for tests

1.0.0

  • Breaking API changes: New feature allows catalog directory (relative to process.cwd()) to be provided) -- Is actually required more than "allowed"
  • Syntax Style updates
  • Fixed minor logic in default catalog behavior
  • More unit tests
  • Ghost implementation of allowing catalog languages to be specified (expect functionality in future release)

0.0.2

  • Initial release
  • Not very useful
  • Shipped quickly and had unit tests!

Package Sidebar

Install

npm i l10n-translator

Weekly Downloads

2

Version

1.1.0

License

ISC

Last publish

Collaborators

  • sprjrx