A JavaScript implementation of gettext, a localization framework.
Installations
npm install node-gettext
Releases
Unable to fetch releases
Developer
alexanderwallin
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
12.11.1
NPM Version
6.11.3
Statistics
188 Stars
136 Commits
37 Forks
7 Watching
9 Branches
11 Contributors
Updated on 04 Jan 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
26,009,066
Last day
-16.3%
26,437
Compared to previous day
Last week
8.2%
142,454
Compared to previous week
Last month
7.4%
557,500
Compared to previous month
Last year
-6.8%
6,446,075
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
node-gettext
node-gettext
is a JavaScript implementation of (a large subset of) gettext, a localization framework originally written in C.
If you just want to parse or compile mo/po files, for use with this library or elsewhere, check out gettext-parser.
NOTE: This is the README for v2 of node-gettext, which introduces several braking changes. You can find the README for v1 here.
Features
- Supports domains, contexts and plurals
- 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
andLC_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 theLC_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)
andsetlocale(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
1npm install --save node-gettext
Usage
1import Gettext from 'node-gettext' 2import swedishTranslations from './translations/sv-SE.json' 3 4const gt = new Gettext() 5gt.addTranslations('sv-SE', 'messages', swedishTranslations) 6gt.setLocale('sv-SE') 7 8gt.gettext('The world is a funny place') 9// -> "Världen är en underlig plats"
Error events
1// Add translations etc... 2 3gt.on('error', error => console.log('oh nose', error)) 4gt.gettext('An unrecognized message') 5// -> 'oh nose', 'An unrecognized message'
Recipes
Load and add translations from .mo or .po files
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:
1import fs from 'fs' 2import path from 'path' 3import Gettext from 'node-gettext' 4import { po } from 'gettext-parser' 5 6// In this example, our translations are found at 7// path/to/locales/LOCALE/DOMAIN.po 8const translationsDir = 'path/to/locales' 9const locales = ['en', 'fi-FI', 'sv-SE'] 10const domain = 'messages' 11 12const gt = new Gettext() 13 14locales.forEach((locale) => { 15 const fileName = `${domain}.po` 16 const translationsFilePath = path.join(translationsDir, locale, fileName) 17 const translationsContent = fs.readFileSync(translationsFilePath) 18 19 const parsedTranslations = po.parse(translationsContent) 20 gt.addTranslations(locale, domain, parsedTranslations) 21})
API
Gettext
- Gettext
- new Gettext([options])
- .on(eventName, callback)
- .off(eventName, callback)
- .addTranslations(locale, domain, translations)
- .setLocale(locale)
- .setTextDomain(domain)
- .gettext(msgid) ⇒
String
- .dgettext(domain, msgid) ⇒
String
- .ngettext(msgid, msgidPlural, count) ⇒
String
- .dngettext(domain, msgid, msgidPlural, count) ⇒
String
- .pgettext(msgctxt, msgid) ⇒
String
- .dpgettext(domain, msgctxt, msgid) ⇒
String
- .npgettext(msgctxt, msgid, msgidPlural, count) ⇒
String
- .dnpgettext(domain, msgctxt, msgid, msgidPlural, count) ⇒
String
- .textdomain()
- .setlocale()
.addTextdomain()
new Gettext([options])
Creates and returns a new Gettext instance.
Returns: Object
- A Gettext instance
Params
[options]
:Object
- A set of options.sourceLocale
:String
- The locale that the source code and its texts are written in. Translations for this locale is not necessary..debug
:Boolean
- Whether to output debug info into the console.
gettext.on(eventName, callback)
Adds an event listener.
Params
eventName
:String
- An event namecallback
:function
- An event handler function
gettext.off(eventName, callback)
Removes an event listener.
Params
eventName
:String
- An event namecallback
:function
- A previously registered event handler function
gettext.addTranslations(locale, domain, translations)
Stores a set of translations in the set of gettext catalogs.
Params
locale
:String
- A locale stringdomain
:String
- A domain nametranslations
:Object
- An object of gettext-parser JSON shape
Example
1gt.addTranslations('sv-SE', 'messages', translationsObject)
gettext.setLocale(locale)
Sets the locale to get translated messages for.
Params
locale
:String
- A locale
Example
1gt.setLocale('sv-SE')
gettext.setTextDomain(domain)
Sets the default gettext domain.
Params
domain
:String
- A gettext domain name
Example
1gt.setTextDomain('domainname')
gettext.gettext(msgid) ⇒ String
Translates a string using the default textdomain
Returns: String
- Translation or the original string if no translation was found
Params
msgid
:String
- String to be translated
Example
1gt.gettext('Some text')
gettext.dgettext(domain, msgid) ⇒ String
Translates a string using a specific domain
Returns: String
- Translation or the original string if no translation was found
Params
domain
:String
- A gettext domain namemsgid
:String
- String to be translated
Example
1gt.dgettext('domainname', 'Some text')
gettext.ngettext(msgid, msgidPlural, count) ⇒ String
Translates a plural string using the default textdomain
Returns: String
- Translation or the original string if no translation was found
Params
msgid
:String
- String to be translated when count is not pluralmsgidPlural
:String
- String to be translated when count is pluralcount
:Number
- Number count for the plural
Example
1gt.ngettext('One thing', 'Many things', numberOfThings)
gettext.dngettext(domain, msgid, msgidPlural, count) ⇒ String
Translates a plural string using a specific textdomain
Returns: String
- Translation or the original string if no translation was found
Params
domain
:String
- A gettext domain namemsgid
:String
- String to be translated when count is not pluralmsgidPlural
:String
- String to be translated when count is pluralcount
:Number
- Number count for the plural
Example
1gt.dngettext('domainname', 'One thing', 'Many things', numberOfThings)
gettext.pgettext(msgctxt, msgid) ⇒ String
Translates a string from a specific context using the default textdomain
Returns: String
- Translation or the original string if no translation was found
Params
msgctxt
:String
- Translation contextmsgid
:String
- String to be translated
Example
1gt.pgettext('sports', 'Back')
gettext.dpgettext(domain, msgctxt, msgid) ⇒ String
Translates a string from a specific context using s specific textdomain
Returns: String
- Translation or the original string if no translation was found
Params
domain
:String
- A gettext domain namemsgctxt
:String
- Translation contextmsgid
:String
- String to be translated
Example
1gt.dpgettext('domainname', 'sports', 'Back')
gettext.npgettext(msgctxt, msgid, msgidPlural, count) ⇒ String
Translates a plural string from a specific context using the default textdomain
Returns: String
- Translation or the original string if no translation was found
Params
msgctxt
:String
- Translation contextmsgid
:String
- String to be translated when count is not pluralmsgidPlural
:String
- String to be translated when count is pluralcount
:Number
- Number count for the plural
Example
1gt.npgettext('sports', 'Back', '%d backs', numberOfBacks)
gettext.dnpgettext(domain, msgctxt, msgid, msgidPlural, count) ⇒ String
Translates a plural string from a specifi context using a specific textdomain
Returns: String
- Translation or the original string if no translation was found
Params
domain
:String
- A gettext domain namemsgctxt
:String
- Translation contextmsgid
:String
- String to be translatedmsgidPlural
:String
- If no translation was found, return this on count!=1count
:Number
- Number count for the plural
Example
1gt.dnpgettext('domainname', 'sports', 'Back', '%d backs', numberOfBacks)
gettext.textdomain()
C-style alias for setTextDomain
gettext.setlocale()
C-style alias for setLocale
gettext.addTextdomain()
Deprecated
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 nowsetLocale(locale)
dgettext
,dngettext
,dpgettext
anddnpgettext
does not treat the leadingdomain
argument as a locale, but as a domain. To get a translation from a certain locale you need to callsetLocale(locale)
beforehand.- A new
setTextDomain(domain)
has been introduced addTextdomain(domain, file)
is nowaddTranslations(locale, domain, translations)
addTranslations(locale, domain, translations)
only accepts a JSON object with the shape described in thegettext-parser
README. To load translations from .mo or .po files, use gettext-parser, and it will provide you with valid JSON objects._currentDomain
is nowdomain
domains
is nowcatalogs
- The instance method
__normalizeDomain(domain)
has been replaced by a static methodGettext.getLanguageCode(locale)
License
MIT
See also
- gettext-parser - Parsing and compiling gettext translations between .po/.mo files and JSON
- lioness – Gettext library for React
- react-gettext-parser - Extracting gettext translatable strings from JS(X) code
- narp - Workflow CLI tool that syncs translations between your app and Transifex
Stable Version
The latest stable version of the package.
Stable Version
3.0.0
HIGH
1
5.9/10
Summary
node-gettext vulnerable to Prototype Pollution
Affected Versions
<= 3.0.0
Patched Versions
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Warn: project license file does not contain an FSF or OSI license.
Reason
Found 2/15 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 17 are checked with a SAST tool
Reason
24 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-2j2x-2gpw-g8fm
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-j383-35pm-c5h4
- Warn: Project is vulnerable to: GHSA-rm36-94g8-835r
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-6vfc-qv3f-vr6c
- Warn: Project is vulnerable to: GHSA-5v2h-r2cx-5xgj
- Warn: Project is vulnerable to: GHSA-rrrm-qjm4-v8hf
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-g6ww-v8xp-vmwg
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4rq4-32rv-6wp6
- Warn: Project is vulnerable to: GHSA-64g7-mvw6-v9qj
- Warn: Project is vulnerable to: GHSA-mxhp-79qh-mcx6
- Warn: Project is vulnerable to: GHSA-cf4h-3jhx-xvhq
Score
1.8
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More