Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including stonejs with all npm packages installed. Try it out:

var stonejs = require("stonejs")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

stonejs v2.4.0

gettext-like client-side Javascript Internationalization Library

Stone.js: Javascript i18n Library

Build Status NPM Version License

Stone.js is a client-side gettext-like Javascript internationalization library that provides many useful functionalities like:

  • immediate translation (gettext)

  • differed translation using lazy strings (lazyGettext)

  • Javascript and HTML internationalization

  • replacement support inside translated strings

  • plural forms support (ngettext/nLazyGettext) soon

  • tools to extract/update/build translatable strings (see stonejs-tools)

Getting Started

Getting Stone.js

Standalone Version

First download Stone.js zip or clone the repository.

Then include the stonejs.js or stonejs.min.js (from the dist folder) in you HTML page, and create an alias for the Stone.gettext function:

<script src="dist/stonejs.js"></script>
<script>
    window._ = Stone.gettext;
</script>

NPM and Browserify

First install the stonejs package:

npm install --save stonejs

Then include it where you need it, and create an alias for the Stone.gettext function:

// using CommonJS modules

var Stone = require("stonejs");
var _ = Stone.gettext;

// using ES6 modules
import Stone, { gettext as _ } from "stonejs";

Internationalize Your Application

Internationalize Javascript

To internationalize your Javascript files, you just have to "mark" all translatable strings of your application by passing them to the gettext function.

Example:

// ---- Replace ----

var text = "Hello World";

// --- by ----

var text = _("Hello World");

// NOTE: you can also write Stone.gettext("Hello World")
// if you do not want to create the "_" alias for the gettext function

Internationalize HTML

To internationalize your HTML files, you just have to add the stonejs attribute to all translatable tags.

Example:

<!-- replace -->

<div>Hello World</div>

<!-- by -->

<div stonejs>Hello World</div>

NOTE: To allow Stone.js to translate your DOM, you have to enable DOM scan:

Stone.enableDomScan(true);

Extract/Translate/Build Translatable Strings

Once you have internationalized your application, you will have to:

  • Extract the translatable strings from your js and html files,

  • Translate the extracted strings,

  • Build your translation inside string catalogs.

For all those steps, you can use the Stone.js tools available here:

  • https://github.com/flozz/stonejs-tools

Load Catalogs / Enable Translation of your application

The last step to display your application into plenty of languages is to load the catalogs you built with stonejs-tools and set the current locale.

NOTE: Stone.js Tools can build the catalogs in two formats: js and json. Be careful if you use the javascript one. You should include the catalog file after the stonejs lib is loaded (you should include it after in your HTML file if you are using the standalone version of the lib, and if you use the npm/browserify version, you should include the file after the first time you require the library).

Example:

<script src="dist/stonejs.js"></script>
<script>
    var catalogs = {
        "fr": {
            "plural-forms": "nplurals=2; plural=(n > 1);",
            "messages": {
                "Hello World": ["Bonjour le monde"],
                "Hello {name}": ["Bonjour {name}"]
            }
        },
        "it": {
            "plural-forms": "nplurals=2; plural=(n != 1);",
            "messages": {
                "Hello World": ["Buongiorno il mondo"],
                "Hello {name}": ["Buongiorno {name}"]
            }
        }
    };

    window._ = Stone.gettext;       // Alias the Stone.gettext function
    Stone.addCatalogs(catalogs);    // Add catalogs (here it is hard coded for the need of the example
    Stone.enableDomScan(true);      // Allow Stone.js to scan the DOM to find translatable nodes
    Stone.setLocale("fr");          // Sets the locale to french


    console.log(_("Hello World"));
    // Bonjour le monde

    console.log(_("Hello {name}", {name: "John"}));
    // Bonjour John


    var text = Stone.lazyGettext("Hello World");

    console.log(text.toString());
    // Bonjour le monde

    Stone.setLocale("it");
    console.log(text.toString());
    // Buongiorno il mondo

    Stone.setLocale("c");
    console.log(text.toString());
    // Hello World

    Stone.setLocale("foo");
    console.log(text.toString());
    // Hello World

</script>

API

Stone.gettext

Translates the given string to the current language.

String: Stone.gettext( <string> [, replacements] );

params:

  • string: The string to translate.
  • replacements: an object containing replacements for the string (optional, see example below).

returns:

The translated string.

Examples:

var text1 = Stone.gettext("Hello World");
var text2 = Stone.gettext("Hello {name}", {name: "John"});

Stone.lazyGettext

Same as Stone.gettext but returns a Stone.LazyString instead of a String.

String: Stone.lazyGettext( <string> [, replacements] );

Stone.addCatalogs

Adds one (or more if you merged multiple languages into one file) string catalog.

Stone.addCatalogs( <catalogs> );

params:

  • catalogs: An object containing translated strings (catalogs can be built using [stronejs-tools][]).

Examples:

Stone.addCatalogs(catalogs);

Stone.getLocale

Returns the current locale (aka target language for the gettext and lazyGettext functions). The default locale is "c" (it means no translation: simply returns the string as it is in the source).

String: Stone.getLocale();

Examples:

var locale = Stone.getLocale();
// "c", "en", "fr", ...

Stone.listCatalogs

Returns all availables catalogs.

String: Stone.listCatalogs();

Examples:

var catalogsList = Stone.listCatalogs();
// ["c", "en", "fr", ... ]

Stone.setLocale

Defines the current locale (aka the target language for the gettext and lazyGettext functions).

NOTE: You can use the setBestMatchingLocale function to set the best language for the user.

Stone.setLocale( <locale> );

params:

  • locale: The locale code (e.g. en, fr, ...)

Examples:

Stone.setLocale("fr");

Stone.setBestMatchingLocale

Find and set the best language for the user (depending on available catalogs and given language list).

Stone.setBestMatchingLocale( [locales] );

params:

  • locales: (optional) string or array of string (e.g. "fr", ["fr", "fr_FR", "en_US"]).

Examples:

Stone.setBestMatchingLocale();  // Automaticaly set the best language (from informations given by the browser)
setBestMatchingLocale("fr");    // Finds the catalog that best match "fr" ("fr", "fr_FR", fr_*,...)
setBestMatchingLocale(["fr", "en_US", "en_UK"]);    // Finds the best available catalog from the given list

Stone.findBestMatchingLocale

Find and return the given locale that best matches the given catalogs.

Stone.findBestMatchingLocale( [locales], [catalogs] );

params:

  • locales: string or array of string (e.g. "fr", ["fr", "fr_FR", "en_US"]).
  • catalogs: array of string (e.g. ["fr_FR", "en"]).

Example:

Stone.findBestMatchingLocale(["fr"], ["pt_BR", "fr_CA", "fr_FR"]);  // -> "fr_FR"

Stone.guessUserLanguage

Tries to guess the user language (based on the browser's preferred languages).

String: Stone.guessUserLanguage();

returns:

The user's language.

example:

var locale = Stone.guessUserLanguage();

Stone.enableDomScan

Allows Stone.js to scan all the DOM to find translatable strings (and to translate them).

Stone.enableDomScan( <enable> );

params:

  • enable: Enable the scan of the DOM if true, disable it otherwise.

example:

Stone.enableDomScan(true);

Stone.updateDomTranslation

Updates the DOM translation if DOM scan was enabled with Stone.enableDomScan (re-scan and re-translate all strings).

Stone.updateDomTranslation();

Stone.LazyString (class)

Stone.LazyString is an object returned by the Stone.lazyGettext function. It behaves like a standard String object (same API) but its value changes if you change the locale with Stone.setLocale function.

This is useful when you have to define translatable strings before the string catalog was loaded, or to automatically re-translate strings each time the locale is changed.

You can find an example of its use in the PhotonUI documentation:

  • http://wanadev.github.io/PhotonUI/doc/widgets/translation.html

"stonejs-locale-changed" (event)

This event is fired each time the locale changes (using the Stone.setLocale function).

Example Catalogs (JSON)

{
    "fr": {
        "plural-forms": "nplurals=2; plural=(n > 1);",
        "messages": {
            "Hello World": ["Bonjour le monde"],
            "Hello {name}": ["Bonjour {name}"]
        }
    },
    "it": {
        "plural-forms": "nplurals=2; plural=(n != 1);",
        "messages": {
            "Hello World": ["Buongiorno il mondo"],
            "Hello {name}": ["Buongiorno {name}"]
        }
    }
}

Changelog

  • 2.4.0:

    • Adds the listCatalogs methods to list available catalogs (thanx @BobRazowsky, #12)
  • 2.3.0:

    • The addCatalog function now merge catalogs.
  • 2.2.0:

    • New function to find the best matching catalog from given locales and catalogs (findBestMatchingLocale)
  • 2.1.0:

    • Better language code handling (now support locales with dialect like fr_FR, fr_CA,...)
    • New function to select the best catalog from a language list (setBestMatchingLocale)
  • 2.0.0:

    • new javascript tools to replace the old pythonic ones
    • new file format (incompatible with the previous version!) to be ready for plural forms (ngettext) support
    • new documentation

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free