A GNU gettext implementation for node.js
node-gettext brings the power of the GNU gettext API to your node.js application.
Simply require the library, load a few *.po files and start using any of the gettext interfaces to translate your strings:
var gettext = require('gettext'), _ = gettext.gettext; gettext.loadLanguageFile('./locale/de/messages.po', 'de'); gettext.loadLanguageFile('./locale/fr/messages.po', 'fr'); gettext.setlocale('LC_ALL', 'de'); console.log(_('Hello, World!')); gettext.setlocale('LC_ALL', 'fr'); console.log(_('Hello, World!'));
Install via npm
npm install node-gettext
setlocale(category, locale) - Sets the locale application wide. If
locale is the empty string, locale will be set to the default value, which is one of the following:
category is not used. Set to any value. All subsequent calls to any gettext function will now try to translate to the specified locale.
textdomain([domain]) - Returns the currently active domain. If
domain was specified, sets the currently active domain to
domainbefore returning it.
loadLanguageFile(file, [locale], [callback]) - Loads the specified *.po
file. The translation table of the specified
locale will be populated with the data found in the
file. The textdomain will be set according to the filename. If
locale was not specified, the data will be stored in the currently active translation table set by
callback was specified it will be called after the translation data was loaded. Since all calls to gettext translation functions will fallback gracefully and return the given string if no applicable translation data was found, waiting for the translation data to be fully loaded before continueing is not required.
loadLocaleDirectory(directory, [callback]) - Loads translation data from the given
directory tree. The function will look for subdirectories in the given
directory. Those directories' names will be mapped to language/locale identifiers. Then each *.po file in each of those subdirectories will be loaded, using the filename as the textdomain. If
callback is given, it will be called after all files and directories where loaded. Again, due to gracefull fallback it is not necceseraly required to wait for the loading function to finish. An example directory structure could look like follows:
Directory | |- en | \ | |- messages.po | |- other.po | o | |- de | \ | |- messages.po | |- other.po | o | |- fr o \ |- messages.po |- other.po o
directory "Directory" contains 4 subdirectories "en", "de" and "fr" which represent languages and can be selected using
setlocale. Each subdirectory itself contains 2 *.po files "messages.po" and "other.po" which can be selected using the
gettext(string) - Returns the translation for the given
string. The translation is determined by evaluating the currently active locale and domain. If no valid translation is found, the given
string is returned instead.
ngettext(singular, plural, n) - Returns the translation for the given
plural form. Which translation is returned is determined by the amount
n and the plural forms header.