GetText support for Node.js and Component
Lightweight GetText for Javascript
This module provides GetText support for both CommonJS and the browser.
Categories and locales are not supported, and will never be. It is assumed that the content currently loaded into GetText matches the current locale.
Server-side example
var fs = ;var gettext = ; { console;} gettext;gettext; console;console;
Browser example
var gettext = ; { console;} /** * 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 */ { var numPOFiles = data && datalength >>> 0; while numPOFiles > 0 var po = data--numPOFiles; // if po.domainName is undefined or null, the default domain ('messages') is used gettext; // Set the default domain gettext; // Output the translation of "person" based on the number from the default domain console; // Output the translation of "cat" from the domain "animal" console;}
Note: we are using Component for the frontend
API
Load a string from default language file
gettext(msgid)
var greeting = gettext;
Equivalent to
gettext.dpngettext(null, null, msgid, null, null)
.
Load a plural string from default language file
ngettext(msgid, msgid_plural, n)
gettext;
Equivalent to
gettext.dpngettext(null, null, msgid, msgid_plural, n)
.
Load a string from a specific language file
dgettext(domain, msgid)
var greeting = gettext;
Equivalent to
gettext.dpngettext(domain, null, msgid, null, null)
.
Load a plural string from a specific language file
dngettext(domain, msgid, msgid_plural, n)
gettext;
Equivalent to
gettext.dpngettext(domain, null, msgid, msgid_plural, n)
.
Load a string of a specific context
pgettext(msgctxt, msgid)
gettext;
Equivalent to
gettext.dpngettext(null, msgctxt, msgid, null, null)
.
Load a plural string of a specific context
pngettext(msgctxt, msgid, msgid_plural, n)
gettext;
Equivalent to
gettext.dpngettext(null, msgctxt, msgid, msgid_plural, n)
.
Load a string of a specific context from specific language file
dpgettext(domain, msgctxt, msgid)
gettext;
Equivalent to
gettext.dpngettext(domain, msgctxt, msgid, null, null)
.
Load a plural string of a specific context from specific language file
dpngettext(domain, msgctxt, msgid, msgid_plural, n)
gettext;
Bind the source (.po file) to a specific domain.
gettext.loadDomain(domain, source, error_handler)
error_handler
will be called every time an error is encountered.
Bind the source with the domain 'message'
gettext.load(source, error_handler)
Equivalent to
gettext.loadDomain('messages', source, error_handler)
Release all data bound to the domain 'messages'
release()
Equivalent to
gettext.releaseDomain('messages')
.
Releases all data bound to the specified domain.
releaseDomain(domain)
Release all data
reset()
Set a default domain
textdomain(domain)
gettext;
Generate a .po file
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)