Node.js Gettext Localization Library

3.0.1 · active · verified Sun Apr 19

node-gettext is a comprehensive JavaScript implementation of a significant subset of the GNU gettext localization framework, designed for both server-side Node.js and client-side browser environments, making it an isomorphic solution. Currently at version 3.0.1, the library focuses exclusively on string and phrase translation, omitting categories like LC_NUMERIC or LC_MONETARY found in the full GNU gettext specification, as it assumes the LC_MESSAGES category at all times. It supports core gettext features such as domains, contexts, and plural forms, and ships with pluralization rules for 136 languages. A key differentiator is that developers are responsible for loading translation files (e.g., .mo, .po, .json via gettext-parser) and providing them to the instance, rather than the library automatically reading from the file system. The library provides useful error messages when debug is enabled and emits events for internal issues, such as missing translations.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic setup, adding translations from an in-memory object, setting the active locale, and translating singular, plural, and interpolated strings. It also includes an example of handling error events for missing translations.

import Gettext from 'node-gettext';

// Example Swedish translation data in the format expected by node-gettext.
// This would typically be loaded from a .json, .mo, or .po file processed by gettext-parser.
const swedishTranslations = {
  charset: 'UTF-8',
  headers: {
    'plural-forms': 'nplurals=2; plural=(n != 1);',
    'content-type': 'text/plain; charset=UTF-8'
  },
  translations: {
    messages: {
      'The world is a funny place': {
        msgid: 'The world is a funny place',
        msgstr: ['Världen är en underlig plats']
      },
      'Hello, %s!': {
        msgid: 'Hello, %s!',
        msgstr: ['Hallå, %s!']
      },
      'One apple': {
        msgid: 'One apple',
        msgid_plural: '%d apples',
        msgstr: ['Ett äpple', '%d äpplen']
      }
    }
  }
};

const gt = new Gettext();

// Add the loaded translations for the 'messages' domain and 'sv-SE' locale.
gt.addTranslations('sv-SE', 'messages', swedishTranslations);

// Set the current locale for the Gettext instance.
gt.setLocale('sv-SE');

console.log('--- Simple Translation ---');
console.log(gt.gettext('The world is a funny place'));
// Expected output: "Världen är en underlig plats"

console.log('\n--- Translation with Interpolation ---');
console.log(gt.gettext('Hello, %s!', 'Alex'));
// Expected output: "Hallå, Alex!"

console.log('\n--- Plural Form Translation (Singular) ---');
console.log(gt.ngettext('One apple', '%d apples', 1));
// Expected output: "Ett äpple"

console.log('\n--- Plural Form Translation (Plural) ---');
console.log(gt.ngettext('One apple', '%d apples', 5));
// Expected output: "5 äpplen"

// Example of error event handling for missing translations
gt.on('error', (error) => {
  console.error('\nGettext Error Event:', error);
});

console.log('\n--- Triggering an Error Event (missing translation) ---');
gt.gettext('This message does not exist');
// Expected output: "Gettext Error Event: This message does not exist"

view raw JSON →