Gettext translator
Javascript gettext translator. Use gettext/gettext PHP library to generate and modify the messages.
Supports:
Installation
Using bower:
bower install gettext-translator
Using npm:
npm install gettext-translator
Usage
Use the Json generator of the gettext/gettext library to export the translations to json:
use Gettext\Translations;
$translations = Translations::fromPoFile('locales/gl.po');
$translations->toJsonFile('locales/gl.json');
Load the json file in your browser (for example, using webpack) and use it
var Translator = require('gettext-translator');
var translations = require('locales/gl.json');
var i18n = new Translator(translations);
console.log(i18n.gettext('hello world'));
Sprintf
This library includes sprintf dependency implemented in the short methods like __
, n__
, etc...:
i18n.__('Hello %s', 'world');
i18n.n__('One comment', '%s comments', 12, 12);
API
Long method | Short + sprintf | description |
---|
gettext | __ | Returns a translation |
ngettext | n__ | Returns a translation with singular/plural variations |
dngettext | dn__ | Returns a translation with domain and singular/plural variations |
npgettext | np__ | Returns a translation with context and singular/plural variations |
pgettext | p__ | Returns a translation with a specific context |
dgettext | d__ | Returns a translation with a specific domain |
dpgettext | dp__ | Returns a translation with a specific domain and context |
dnpgettext | dnp__ | Returns a translation with a specific domain, context and singular/plural variations |