Stone.js: JavaScript i18n Library
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/lazyNgettext)
-
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:
var Stone = require("stonejs");
var _ = Stone.gettext;
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:
var text = "Hello World";
var text = _("Hello World");
Internationalize HTML
To internationalize your HTML files, you just have to add the stonejs
attribute to all translatable tags.
Example:
<div>Hello World</div>
<div stonejs>Hello World</div>
NOTE: To allow Stone.js to translate your DOM, you have to enable DOM scan:
Stone.enableDomScan(true);
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:
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;
Stone.addCatalogs(catalogs);
Stone.enableDomScan(true);
Stone.setLocale("fr");
console.log(_("Hello World"));
console.log(_("Hello World", "it"));
console.log(_("Hello {name}", {name: "John"}));
console.log(_("Hello {name}", {name: "John"}, "it"));
var text = Stone.lazyGettext("Hello World");
console.log(text.toString());
Stone.setLocale("it");
console.log(text.toString());
Stone.setLocale("c");
console.log(text.toString());
Stone.setLocale("foo");
console.log(text.toString());
</script>
API
Stone.gettext
Translates the given string to the current language.
String: Stone.gettext( <string> [, locale] );
String: Stone.gettext( <string> [, replacements] [, locale] );
params:
string
: The string to translate.
locale
: The locale string to use for translation (optional, default: current locale).
replacements
: an object containing replacements for the string (optional, see example below).
returns:
The translated string.
Examples:
var text1 = Stone.gettext("Hello World");
var text1 = Stone.gettext("Hello World", "it");
var text2 = Stone.gettext("Hello {name}", {name: "John"});
var text3 = Stone.gettext("Hello {name}", {name: "John"}, "it");
Stone.lazyGettext
Same as Stone.gettext
but returns a Stone.LazyString
instead of a String
.
String: Stone.lazyGettext( <string> [, locale] );
String: Stone.lazyGettext( <string> [, replacements] [, locale] );
Stone.ngettext
Translates the given strings to the current language with plural support.
String: Stone.ngettext( <string>, <stringPlural>, <number> [, locale] );
String: Stone.ngettext( <string>, <stringPlural>, <number> [, replacements] [, locale] );
params:
string
: The string to translate, in English singular form.
stringPlural
: The string to translate, in English plural form.
number
: The number that determines plural forms
locale
: The locale string to use for translation (optional, default: current locale).
replacements
: an object containing replacements for the string (optional, see example below).
Note: 'n' is an implicit replacement for given number.
returns:
The translated string, in some plural form.
Examples:
var text1 = Stone.ngettext("one apple", "{nbApples} apples", 3, {nbApples: 3});
var text2 = Stone.ngettext("{n} apple", "{n} apples", 3, {n: 3});
var text3 = Stone.ngettext("{n} apple", "{n} apples", 3);
Stone.lazyNgettext
Same as Stone.ngettext
but returns a Stone.LazyNString
instead of a String
.
Stone.pgettext
Translates the given string to the current language using a context argument to solve ambiguities (read more).
String: Stone.pgettext( <context>, <string> [, locale] );
String: Stone.pgettext( <context>, <string> [, replacements] [, locale] );
params:
context
: The context of the string.
string
: The string to translate.
locale
: The locale string to use for translation (optional, default: current locale).
replacements
: an object containing replacements for the string (optional).
returns:
The translated string.
Examples:
var text1 = Stone.pgettext("going back", "Back");
var text1 = Stone.pgettext("back of an object", "Back", "it");
Stone.lazyPgettext
Same as Stone.pgettext
but returns a Stone.LazyPString
instead of a String
.
Stone.npgettext
Translates the given strings to the current language with plural support and using a context argument to solve ambiguities (read more).
String: Stone.npgettext( <context>, <string>, <stringPlural>, <number> [, locale] );
String: Stone.npgettext( <context>, <string>, <stringPlural>, <number> [, replacements] [, locale] );
params:
context
: The context of the string.
string
: The string to translate, in English singular form.
stringPlural
: The string to translate, in English plural form.
number
: The number that determines plural forms
locale
: The locale string to use for translation (optional, default: current locale).
replacements
: an object containing replacements for the string (optional, see example below).
Note: 'n' is an implicit replacement for given number.
returns:
The translated string, in some plural form.
Examples:
var text1 = Stone.npgettext("fruit", "one apple", "{nbApples} apples", 3, {nbApples: 3});
var text2 = Stone.npgettext("fruit", "{n} apple", "{n} apples", 3, {n: 3});
var text3 = Stone.npgettext("fruit", "{n} apple", "{n} apples", 3);
Stone.lazyNpgettext
Same as Stone.npgettext
but returns a Stone.LazyNPString
instead of a String
.
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.gettext_noop
Register a string to be translatable.
Do not operate translation.
Translation can be operated later with Stone.gettext
.
This can be useful in special cases where you want to register a string for translation,
but want to keep a reference of the original string, in order to translate it later.
String: Stone.gettext_noop( <string> );
params:
string
: The string to register for translation
returns:
The exact same given string
Examples:
var translatable1 = Stone.gettext_noop("Some string to translate");
var translatable2 = Stone.gettext_noop("Hello {name}");
var text1 = Stone.gettext(translatable1);
var text2 = Stone.gettext(translatable2, { name: "John" });
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();
Stone.listCatalogs
Returns all availables catalogs.
String: Stone.listCatalogs();
Examples:
var catalogsList = Stone.listCatalogs();
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();
setBestMatchingLocale("fr");
setBestMatchingLocale(["fr", "en_US", "en_UK"]);
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"]);
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:
Stone.LazyNString (class)
Same as Stone.LazyString
, using Stone.ngettext
for plural support.
Stone.LazyPString (class)
Same as Stone.LazyString
, using Stone.pgettext
for context support.
Stone.LazyNPString (class)
Same as Stone.LazyString
, using Stone.npgettext
for plural and context support.
"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}"]
}
}
}
}
Support this project
Want to support this project?
Changelog