Gettext client for Node.js to use .mo files for I18N

npm install node-gettext
88 downloads in the last day
257 downloads in the last week
1 576 downloads in the last month


node-gettext is a Node.JS module to use .MO and .PO files.


  • Load binary MO or source PO files
  • Supports contexts and plurals
  • Add your own translations to the list
  • Recompile current translation table into a MO or a PO file!

Build Status

Support node-gettext development

Donate to author


npm install node-gettext


Create a new Gettext object

var Gettext = require("node-gettext");

var gt = new Gettext();

Add a language

addTextdomain(domain, file)

Language data needs to be in the Buffer format - it can be either contents of a MO or PO file.

addTextdomain(domain[, fileContents])

Load from a MO file

var fileContents = fs.readFileSync("");
gt.addTextdomain("et", fileContents);

or load from a PO file

var fileContents = fs.readFileSync("et.po");
gt.addTextdomain("et", fileContents);

If you do not include the file contents, then a blank language template object is created which can be edited with setTranslation, deleteTranslation methods etc.

Plural rules are automatically detected from the language code

gt.setTranslation("et", false, "hello!", "tere!");

Check or change default language



The function also returns the current texdomain value

var curlang = gt.textdomain();

Translation methods

Load a string from default language file


var greeting = gt.gettext("Hello!");

Load a string from a specific language file

dgettext(domain, msgid)

var greeting = gt.dgettext("et", "Hello!");

Load a plural string from default language file

ngettext(msgid, msgid_plural, count)

gt.ngettext("%d Comment", "%d Comments", 10);

Load a plural string from a specific language file

dngettext(domain, msgid, msgid_plural, count)

gt.dngettext("et", "%d Comment", "%d Comments", 10)

Load a string of a specific context

pgettext(msgctxt, msgid)

gt.pgettext("menu items", "File");

Load a string of a specific context from specific language file

dpgettext(domain, msgctxt, msgid)

gt.dpgettext("et", "menu items", "File");

Load a plural string of a specific context

npgettext(msgctxt, msgid, msgid_plural, count)

gt.npgettext("menu items", "%d Recent File", "%d Recent Files", 3);

Load a plural string of a specific context from specific language file

dnpgettext(domain, msgctxt, msgid, msgid_plural, count)

gt.dnpgettext("et", "menu items", "%d Recent File", "%d Recent Files", 3);

Get comments for a translation (if loaded from PO)

getComment(domain, msgctxt, msgid)

gt.getComment("et", "menu items", "%d Recent File");

Returns an object in the form of {comment: "", code: "", note: ""}

Sets a comments for a translation

setComment(domain, msgctxt, msgid, comment)

gt.getComment("et", "menu items", "%d Recent File", "This is a comment");

comment can either be a string or an object with the following properties: {comment: "", code: "", note: ""}.

String helpers

In order to make things really easy, it is possible to attach the gettext functions directly to string prototypes with gettext.registerStringHelpers()


// setup gettext
var gettext = new Gettext();
gettext.addTextdomain("et", fs.readFileSync(""));

// translate any string
var translated = "translate this string".gettext();
// or
var plural = "translate %s string".ngettext("translate %s strings", 10);

// you can even change the default textdomain

The parameters for the gettext functions are the same as with regular gettext methods, except that the msgid parameter is not needed.

Manage translations

Add a translation

setTranslation(domain, context, msgid, translation)

gt.setTranslation("et", "", "Hello", "Tere");

Use an array for plurals

gt.setTranslation("et", "", "%s comments", ["%s kommentaar", "%s kommentaari"]);

Remove a translation

deleteTranslation(domain, context, msgid)

gt.deleteTranslation("et", "", "Hello");

List available contexts


var contextStrArr = gt.listContextNames("et");

List translation keys for a context

listKeys([domain], [context])

var originalsStrArr = gt.listKeys("et", "");


Compile to MO

Compile current translation table to a MO file


fs.writeFile("", gt.compileMO("et")); 

Compile to PO

Compile current translation table to a PO file


fs.writeFile("out.po", gt.compilePO("et"));



npm loves you