Skip to content
Javascript gettext translator
Branch: master
Clone or download
Latest commit c7f3308 Jun 12, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
tests added missing asset Jun 10, 2018
.gitignore added a changelog Jun 12, 2018
LICENSE updated license year Jul 27, 2017 removed bower. The new version contains BC, so it's a mayor version Jul 27, 2017
package.json updated version Jun 12, 2018

Gettext translator

Javascript gettext translator. Use gettext/gettext PHP library to generate and modify the messages.


  • AMD
  • CommonJS
  • Global js


npm install gettext-translator


Use the Json generator of the gettext/gettext library to export the translations to json:

use Gettext\Translations;

//Load the po file with the translations
$translations = Translations::fromPoFile('locales/gl.po');

//Export to a json file

Load the json file in your browser (for example, using webpack) and use it

var Translator = require('gettext-translator');
var translations = require('locales/gl.json');

var i18n = new Translator(translations);

console.log(i18n.gettext('hello world')); //ola mundo


This library includes sprintf dependency implemented in the short methods like __, n__, etc...:

i18n.__('Hello %s', 'world'); //Hello world
i18n.n__('One comment', '%s comments', 12, 12); //12 comments


Long method Short + sprintf description
gettext __ Returns a translation
ngettext n__ Returns a translation with singular/plural variations
dngettext dn__ Returns a translation with domain and singular/plural variations
npgettext np__ Returns a translation with context and singular/plural variations
pgettext p__ Returns a translation with a specific context
dgettext d__ Returns a translation with a specific domain
dpgettext dp__ Returns a translation with a specific domain and context
dnpgettext dnp__ Returns a translation with a specific domain, context and singular/plural variations
You can’t perform that action at this time.