- Easy to use
- Loading from *.po files.
- Plural, context and multi-language support
- Batch loading of entire translation directory trees
- LGPL licensed
electron-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 electron-gettext
setlocale(category, locale)- Sets the locale application wide. If
localeis the empty string, locale will be set to the default value, which is one of the following:
categoryis 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
domainwas 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
localewill be populated with the data found in the
file. The textdomain will be set according to the filename. If
localewas not specified, the data will be stored in the currently active translation table set by
callbackwas 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
directorytree. 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
callbackis 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
stringis returned instead.
ngettext(singular, plural, n)- Returns the translation for the given
pluralform. Which translation is returned is determined by the amount
nand the plural forms header.
import(Object)- Imports the parsed translations into gettext (this will make
loadLocaleDirectoryunnecessary). E.g. you can parse PO files in a build job or at first start of your application and saves parsed json on disk. From your second start of application you can load json file and only imports this json object into electron-gettext.
export()- Exports the parsed translations from gettext. This could you do e.g. in a build job or at first start of your application. PO Files will normally not change in usage of your application.