
Security News
Open Source Maintainers Demand Ability to Block Copilot-Generated Issues and PRs
Open source maintainers are urging GitHub to let them block Copilot from submitting AI-generated issues and pull requests to their repositories.
@postalsys/gettext
Advanced tools
A JavaScript implementation of gettext, a localization framework
Note: This is a fork of alexanderwallin/node-gettext with additional enhancements and maintenance updates.
@postalsys/gettext
is a JavaScript implementation of a substantial subset of GNU gettext, a powerful localization framework originally written in C. This library enables developers to internationalize their JavaScript applications by handling translations, plural forms, and contexts, closely mirroring the functionality of the original gettext.
If you're interested in parsing or compiling .mo
/.po
files for use with this library or elsewhere, consider using gettext-parser.
.json
, .mo
, and .po
files via gettext-parser.debug
option is enabled.While @postalsys/gettext
aims to closely emulate GNU gettext, there are notable differences:
LC_MESSAGES
, LC_NUMERIC
, and LC_MONETARY
, this library focuses solely on message localization, effectively always operating under the LC_MESSAGES
category. For number formatting, currencies, dates, and other localization needs, consider using dedicated JavaScript libraries.@postalsys/gettext
requires developers to manually load and provide translations, accommodating both server-side and client-side environments.Install the package via npm:
npm install @postalsys/gettext
const Gettext = require('@postalsys/gettext');
const swedishTranslations = require('./translations/sv-SE.json');
const gt = new Gettext();
gt.addTranslations('sv-SE', 'messages', swedishTranslations);
gt.setLocale('sv-SE');
console.log(gt.gettext('The world is a funny place'));
// Output: "VΓ€rlden Γ€r en underlig plats"
@postalsys/gettext
emits an error
event when it encounters issues such as missing translations. You can handle these events as follows:
// Set up your Gettext instance and add translations...
gt.on('error', error => {
console.error('Translation error:', error.message);
});
gt.gettext('An unrecognized message');
// Logs: 'Translation error: No translation found for msgid "An unrecognized message"'
@postalsys/gettext
works seamlessly with translations parsed by gettext-parser
. Here's how you can load translations from .po
files:
const fs = require('fs');
const path = require('path');
const Gettext = require('@postalsys/gettext');
const { po } = require('gettext-parser');
// Directory where your translations are stored
const translationsDir = 'path/to/locales';
const locales = ['en', 'fi-FI', 'sv-SE'];
const domain = 'messages';
const gt = new Gettext();
locales.forEach(locale => {
const filename = `${domain}.po`;
const translationsFilePath = path.join(translationsDir, locale, filename);
const translationsContent = fs.readFileSync(translationsFilePath);
const parsedTranslations = po.parse(translationsContent);
gt.addTranslations(locale, domain, parsedTranslations);
});
gt.setLocale('sv-SE');
console.log(gt.gettext('Hello'));
// Outputs the translated string in Swedish
String
String
String
String
String
String
String
String
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.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 functionStores 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)
This project is licensed under the MIT License. See the LICENSE file for details.
.po
/.mo
files and JSON.FAQs
A JavaScript implementation of gettext, a localization framework
The npm package @postalsys/gettext receives a total of 7,304 weekly downloads. As such, @postalsys/gettext popularity was classified as popular.
We found that @postalsys/gettext demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Β It has 1 open source maintainer 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.
Security News
Open source maintainers are urging GitHub to let them block Copilot from submitting AI-generated issues and pull requests to their repositories.
Research
Security News
Malicious Koishi plugin silently exfiltrates messages with hex strings to a hardcoded QQ account, exposing secrets in chatbots across platforms.
Research
Security News
Malicious PyPI checkers validate stolen emails against TikTok and Instagram APIs, enabling targeted account attacks and dark web credential sales.