gettext-like client-side Javascript Internationalization Library
npm install stonejs


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][])
#### Standalone Version
First download [Stone.js zip][dl-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:
``html`
#### 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:
`javascript
// using CommonJS modules
var Stone = require("stonejs");
var _ = Stone.gettext;
// using ES6 modules
import Stone, { gettext as _ } from "stonejs";
`
#### 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:
`javascript
// ---- 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:
`html
NOTE: To allow Stone.js to translate your DOM, you have to enable DOM scan:
`javascript
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:
`html
`
API
$3
Translates the given string to the current language.
String: Stone.gettext( [, locale] );
String: Stone.gettext( [, 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:
`javascript
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");
`
$3
Same as
Stone.gettext but returns a Stone.LazyString instead of a String. String: Stone.lazyGettext( [, locale] );
String: Stone.lazyGettext( [, replacements] [, locale] );
$3
Translates the given strings to the current language with plural support.
String: Stone.ngettext( , , [, locale] );
String: Stone.ngettext( , , [, 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:
`javascript
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); // 'n' is an implicit replacement of given number
`
$3
Same as
Stone.ngettext but returns a Stone.LazyNString instead of a String.
$3
Translates the given string to the current language using a context argument to solve ambiguities (read more).
String: Stone.pgettext( , [, locale] );
String: Stone.pgettext( , [, 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:
`javascript
var text1 = Stone.pgettext("going back", "Back");
var text1 = Stone.pgettext("back of an object", "Back", "it");
`$3
Same as
Stone.pgettext but returns a Stone.LazyPString instead of a String.
$3
Translates the given strings to the current language with plural support and using a context argument to solve ambiguities (read more).
String: Stone.npgettext( , , , [, locale] );
String: Stone.npgettext( , , , [, 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:
`javascript
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); // 'n' is an implicit replacement of given number
`
$3
Same as
Stone.npgettext but returns a Stone.LazyNPString instead of a String.
$3
Adds one (or more if you merged multiple languages into one file) string catalog.
Stone.addCatalogs( );
params:
*
catalogs: An object containing translated strings (catalogs can be built using [stronejs-tools][]).Examples:
`javascript
Stone.addCatalogs(catalogs);
`
$3
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( );
params:
*
string: The string to register for translationreturns:
The exact same given string
Examples:
`javascript
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" });
`
$3
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:
`javascript
var locale = Stone.getLocale();
// "c", "en", "fr", ...
`
$3
Returns all availables catalogs.
String: Stone.listCatalogs();
Examples:
`javascript
var catalogsList = Stone.listCatalogs();
// ["c", "en", "fr", ... ]
`
$3
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( );
params:
*
locale: The locale code (e.g. en, fr, ...)Examples:
`javascript
Stone.setLocale("fr");
`
$3
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:
`javascript
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
`
$3
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:
`javascript
Stone.findBestMatchingLocale(["fr"], ["pt_BR", "fr_CA", "fr_FR"]); // -> "fr_FR"
`
$3
Tries to guess the user language (based on the browser's preferred languages).
String: Stone.guessUserLanguage();
returns:
The user's language.
example:
`javascript
var locale = Stone.guessUserLanguage();
`
$3
Allows Stone.js to scan all the DOM to find translatable strings (and to translate them).
Stone.enableDomScan( );
params:
*
enable: Enable the scan of the DOM if true, disable it otherwise.example:
`javascript
Stone.enableDomScan(true);
`
$3
Updates the DOM translation if DOM scan was enabled with
Stone.enableDomScan (re-scan and re-translate all strings). Stone.updateDomTranslation();
$3
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
$3
Same as
Stone.LazyString, using Stone.ngettext for plural support.
$3
Same as
Stone.LazyString, using Stone.pgettext for context support.
$3
Same as
Stone.LazyString, using Stone.npgettext for plural and context support.
$3
This event is fired each time the locale changes (using the
Stone.setLocale function).
Example Catalogs (JSON)
`javascript
{
"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?
* ☕️ Buy me a coffee
* 💵️ Give me a tip on PayPal
* ❤️ Sponsor me on GitHub
Changelog
* [NEXT] (changes on
master but not released yet): * Nothing yet ;)
* v2.7.0:
* Added support of string with context (
pgettext, npgettext,...) (@Krenodeno, #33, #40)
* Updated dependencies
* Dev: tests are now using Jest instead of Jasmine, and Github Actions replaces Travis* v2.6.0:
* Added support of
ngettext() for the JS API (thanks @jbghoul, #17)
* Added support of gettext_noop() for the JS API (thanks @jbghoul, #19)* v2.5.0:
* Allow to override the local for a particular
gettext() call (thanks @JochLAin, #13)* v2.4.0:
* Added the
listCatalogs methods to list available catalogs (thanks @BobRazowsky, #12)* v2.3.0:
* The
addCatalog function now merge catalogs.* v2.2.0:
* New function to find the best matching catalog from given locales and catalogs (
findBestMatchingLocale)* v2.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`)* v2.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
[stonejs-tools]: https://github.com/flozz/stonejs-tools
[dl-zip]: https://github.com/flozz/stone.js/archive/master.zip