Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
node-gettext
Advanced tools
node-gettext
is a JavaScript implementation of gettext, a localization framework.
If you just want to parse or compile mo/po files, check out gettext-parser.
NOTE: This is the README for v2 of node-gettext, which introduces many braking changes and is currently in alpha. You can find the README for v1 here.
debug
optionnpm install --save node-gettext
import Gettext from 'node-gettext'
import swedishTranslations from './translations/sv-SE.json'
const gt = new Gettext()
gt.addTranslations('sv-SE', 'messages', swedishTranslations)
gt.setLocale('sv-SE')
gt.gettext('The world is a funny place')
// -> "Världen är en underlig plats"
// Add translations etc...
gt.on('error', error => console.log('oh nose', error))
gt.gettext('An unrecognized message')
// -> 'oh nose', 'An unrecognized message'
## 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.setTextDomain(domain)
has been introducedaddTextdomain(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
__normalizeDomain(domain)
has been replaced by a static method Gettext.getLanguageCode(locale)
Creates and returns a new Gettext instance.
Returns: Object
- A Gettext instance
Params
options
: Object
- A set of options
.debug
: Boolean
- Whether to output debug info into the
console.Adds an event listener.
Params
eventName
: String
- An event namecallback
: function
- An event handler functionRemoves an event listener.
Params
eventName
: String
- An event namecallback
: function
- A previously registered event handler functionEmits an event to all registered event listener.
Params
eventName
: String
- An event nameeventData
: any
- Data to pass to event listenersStores 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 shapeExample
gt.addTranslations('sv-SE', 'messages', translationsObject)
Sets the locale to get translated messages for.
Params
locale
: String
- A localeExample
gt.setLocale('sv-SE')
Sets the default gettext domain.
Params
domain
: String
- A gettext domain nameExample
gt.setTextDomain('domainname')
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 translatedExample
gt.gettext('Some text')
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 translatedExample
gt.dgettext('domainname', 'Some text')
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 pluralExample
gt.ngettext('One thing', 'Many things', numberOfThings)
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 pluralExample
gt.dngettext('domainname', 'One thing', 'Many things', numberOfThings)
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 translatedExample
gt.pgettext('sports', 'Back')
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 translatedExample
gt.dpgettext('domainname', 'sports', 'Back')
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 pluralExample
gt.npgettext('sports', 'Back', '%d backs', numberOfBacks)
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 pluralExample
gt.dnpgettext('domainname', 'sports', 'Back', '%d backs', numberOfBacks)
Object
Retrieves comments object for a translation. The comments object
has the shape { translator, extracted, reference, flag, previous }
.
Returns: Object
- Comments object or false if not found
Params
domain
: String
- A gettext domain namemsgctxt
: String
- Translation contextmsgid
: String
- String to be translatedExample
const comment = gt.getComment('domainname', 'sports', 'Backs')
Deprecated
This function will be removed in the final 2.0.0 release.
Deprecated
This function will be removed in the final 2.0.0 release.
String
Returns the language code part of a locale
Returns: String
- A language code
Params
locale
: String
- A case-insensitive locale stringExample
Gettext.getLanguageCode('sv-SE')
// -> "sv"
MIT
FAQs
A JavaScript implementation of gettext, a localization framework
The npm package node-gettext receives a total of 66,341 weekly downloads. As such, node-gettext popularity was classified as popular.
We found that node-gettext demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.