This module provides GetText support for both Node.js and the Component.
Categories and locales are not supported, and will never be. It is assumed that the content currently loaded into GetText matches the current locale.
var fs = require('fs');
var gettext = require('gettextlight');
function errorHandler(err) {
console.log('[GetText][' + err.domain_name + '][#' + err.line + '] ' + err.message);
}
gettext.load(fs.readFileSync('en/messages.po', 'utf-8'), errorHandler);
gettext.loadDomain('domain1', fs.readFileSync('en/domain1.po', 'utf-8'), errorHandler);
console.log(gettext.ngettext('person', 'persons', 12));
console.log(gettext.dggettext('domain1', fs.readFileSync('ja/domain1.po', 'utf-8'), 'cat'));
var gettext = require('gettextlight');
function errorHandler(err) {
console.log('[GetText][' + err.domain_name + '][#' + err.line + '] ' + err.message);
}
/**
* Called on getting data from the server
* The parameter data is an array of object contening the domainName and the content of the po file
*/
function on_localization_data(data) {
var numPOFiles = data && data.length >>> 0;
while (numPOFiles > 0) {
var po = data[--numPOFiles];
// if po.domainName is undefined or null, the default domain ('messages') is used
gettext.loadDomain(po.domainName, po.content, errorHandler);
}
// Set the default domain
gettext.textdomain('nature');
// Output the translation of "person" based on the number from the default domain
console.log(gettext.ngettext('person', 'persons', 12));
// Output the translation of "cat" from the domain "animal"
console.log(gettext.dgettext('animal', 'cat'));
}
Note: we are using Component for the frontend
gettext(msgid)
var greeting = gettext.gettext("Hello!");
Equivalent to
gettext.dpngettext(null, null, msgid, null, null)
.
ngettext(msgid, msgid_plural, n)
gettext.ngettext("Comment", "Comments", 10);
Equivalent to
gettext.dpngettext(null, null, msgid, msgid_plural, n)
.
dgettext(domain, msgid)
var greeting = gettext.dgettext("ja", "Hello!");
Equivalent to
gettext.dpngettext(domain, null, msgid, null, null)
.
dngettext(domain, msgid, msgid_plural, n)
gettext.dngettext("ja", "Comment", "Comments", 10);
Equivalent to
gettext.dpngettext(domain, null, msgid, msgid_plural, n)
.
pgettext(msgctxt, msgid)
gettext.pgettext("menu items", "File");
Equivalent to
gettext.dpngettext(null, msgctxt, msgid, null, null)
.
pngettext(msgctxt, msgid, msgid_plural, n)
gettext.pngettext("menu items", "Recent File", "Recent Files", 3);
Equivalent to
gettext.dpngettext(null, msgctxt, msgid, msgid_plural, n)
.
dpgettext(domain, msgctxt, msgid)
gettext.dpgettext("ja", po, "Cat");
Equivalent to
gettext.dpngettext(domain, msgctxt, msgid, null, null)
.
dpngettext(domain, msgctxt, msgid, msgid_plural, n)
gettext.dpngettext("ja", "menu items", "Recent File", "Recent Files", 3);
gettext.loadDomain(domain, source, error_handler)
error_handler
will be called every time an error is encountered.
gettext.load(source, error_handler)
Equivalent to
gettext.loadDomain('messages', source, error_handler)
release()
Equivalent to
gettext.releaseDomain('messages')
.
releaseDomain(domain)
reset()
textdomain(domain)
gettext.textdomain("ja");
node bin/update-po-files.js <source_path> [-d <dest_path>]
To be able to extract the data, the required object has to be named
gettext
(case insensitive)