Supports .json, .mo and .po files with the help of gettext-parser
Ships with plural forms for 136 languages
Change locale or domain on the fly
Useful error messages enabled by a debug option
Emits events for internal errors, such as missing translations
Differences from GNU gettext
There are two main differences between node-gettext and GNU's gettext:
There are no categories. GNU gettext features categories such as LC_MESSAGES, LC_NUMERIC and LC_MONETARY, but since there already is a plethora of great JavaScript libraries to deal with numbers, currencies, dates etc, node-gettext is simply targeted towards strings/phrases. You could say it just assumes the LC_MESSAGES category at all times.
You have to read translation files from the file system yourself. GNU gettext is a C library that reads files from the file system. This is done using bindtextdomain(domain, localesDirPath) and setlocale(category, locale), where these four parameters combined are used to read the appropriate translations file.
However, since node-gettext needs to work both on the server in web browsers (which usually is referred to as it being universal or isomorphic JavaScript), it is up to the developer to read translation files from disk or somehow provide it with translations as pure JavaScript objects using addTranslations(locale, domain, translations).
bindtextdomain will be provided as an optional feature in a future release.
Installation
npm install --save node-gettext
Usage
importGettextfrom'node-gettext'importswedishTranslationsfrom'./translations/sv-SE.json'constgt=newGettext()gt.addTranslations('sv-SE','messages',swedishTranslations)gt.setLocale('sv-SE')gt.gettext('The world is a funny place')// -> "Världen är en underlig plats"
node-gettext expects all translations to be in the format specified by gettext-parser. Therefor, you should use that to parse .mo or .po files.
Here is an example where we read a bunch of translation files from disk and add them to our Gettext instance:
importfsfrom'fs'importpathfrom'path'importGettextfrom'node-gettext'import{po}from'gettext-parser'// In this example, our translations are found at// path/to/locales/LOCALE/DOMAIN.poconsttranslationsDir='path/to/locales'constlocales=['en','fi-FI','sv-SE']constdomain='messages'constgt=newGettext()locales.forEach((locale)=>{constfileName=`${domain}.po`consttranslationsFilePath=path.join(translationsDir,locale,fileName)consttranslationsContent=fs.readFileSync(translationsFilePath)constparsedTranslations=po.parse(translationsContent)gt.addTranslations(locale,domain,parsedTranslations)})
This function will be removed in the final 2.0.0 release.
Migrating from v1 to v2
Version 1 of node-gettext confused domains with locales, which version 2 has corrected. node-gettext also no longer parses files or file paths for you, but accepts only ready-parsed JSON translation objects.
Here is a full list of all breaking changes:
textdomain(domain) is now setLocale(locale)
dgettext, dngettext, dpgettext and dnpgettext does not treat the leading domain argument as a locale, but as a domain. To get a translation from a certain locale you need to call setLocale(locale) beforehand.
A new setTextDomain(domain) has been introduced
addTextdomain(domain, file) is now addTranslations(locale, domain, translations)
addTranslations(locale, domain, translations)only accepts a JSON object with the shape described in the gettext-parser README. To load translations from .mo or .po files, use gettext-parser, and it will provide you with valid JSON objects.
_currentDomain is now domain
domains is now catalogs
The instance method __normalizeDomain(domain) has been replaced by a static method Gettext.getLanguageCode(locale)
License
MIT
See also
gettext-parser - Parsing and compiling gettext translations between .po/.mo files and JSON
请发表评论