JSPM

  • Created
  • Published
  • Downloads 394852
  • Score
    100M100P100Q160800F

lightweight simple translation module with dynamic json storage

Package Exports

  • i18n

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (i18n) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

i18n

Lightweight simple translation module with dynamic json storage. Uses common __('...') syntax in app and templates. Stores language files in json files compatible to webtranslateit json format. Adds new strings on-the-fly when first used in your app. No extra parsing needed.

Install

npm install i18n

Usage

load and configure with express

in your app.js

// load modules
var express = require('express'),
    i18n = require("i18n");

// register helpers for use in templates
app.helpers({
  __: i18n.__
});

// or even a global for use in your app.js
var __= i18n.__;

simple usage

in your app

var greeting = __('Hello');

in your template (depending on your template compiler)

<%= __('Hello') %>

${__('Hello')}

sprintf support

var greeting = __('Hello %s, how are you today?', 'Marcus');

this puts Hello Marcus, how are you today?. You might add endless arguments and even nest it.

var greeting = __('Hello %s, how are you today? How was your %s.', 'Marcus', __('weekend'));

which puts Hello Marcus, how are you today? How was your weekend.

variable support

you might even use dynamic variables. They get added to the en.js file if not yet existing.

var greetings = ['Hi', 'Hello', 'Howdy'];        
for (var i=0; i < greetings.length; i++) {
    console.log( __(greetings[i]) );
};

which puts

Hi
Hello
Howdy

plural support

different plural froms are supported as response to count:

var singular = __n('%s cat', '%s cats', 1);
var plural = __n('%s cat', '%s cats', 3);

this puts 1 cat or 3 cats and again these could get nested:

var singular = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 1, 'tree');
var plural = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 3, 'tree');

putting There is one monkey in the tree or There are 3 monkeys in the tree

json file

the above will automatically generate a en.js by default inside ./locales/ which looks like

{
    "Hello": "Hello",
    "Hello %s, how are you today?": "Hello %s, how are you today?",
    "weekend": "weekend",
    "Hello %s, how are you today? How was your %s.": "Hello %s, how are you today? How was your %s.",
    "Hi": "Hi",
    "Howdy": "Howdy",
    "%s cat": {
        "one": "%s cat",
        "other": "%s cats"
    },
    "There is one monkey in the %%s": {
        "one": "There is one monkey in the %%s",
        "other": "There are %d monkeys in the %%s"
    },
    "tree": "tree"
}

that file can be edited or just uploaded to webtranslateit for any kind of collaborative translation workflow:

{
    "Hello": "Hallo",
    "Hello %s, how are you today?": "Hallo %s, wie geht es dir heute?",
    "weekend": "Wochenende",
    "Hello %s, how are you today? How was your %s.": "Hallo %s, wie geht es dir heute? Wie war dein %s.",
    "Hi": "Hi",
    "Howdy": "Hallöchen",
    "%s cat": {
        "one": "%s Katze",
        "other": "%s Katzen"
    },
    "There is one monkey in the %%s": {
        "one": "Im %%s sitzt ein Affe",
        "other": "Im Baum sitzen %d Affen"
    },
    "tree": "Baum"
}