Extract gettext strings from __('literal') and __n('single', 'plural', n ) functions in any source

Usage no npm install needed!

<script type="module">
  import extractGettext from 'https://cdn.skypack.dev/extract-gettext';



Build Status

Extract gettext strings from __('literal') and __n('single', 'plural', n ) functions in any source. This module currently only looks for the __ and __n function signatures.

The format produced by this package is similar to the output of xgettext, containing all strings in one flat array. When outputting a .json file, the format is usable by i18next-conv.


npm install extract-gettext



extract-gettext -o <output file> -x <exclude pattern> **/*.js


( as taken from the "test" )

    let scan = require("extract-gettext");

    function x(__, __n) {
        __('This is a singular translation %s', 'replacement');
        __('Plain old singluar');
        __n('single', 'plural', 1, 'cats');
        __n('error, but it works');
        __('one on the same line');
        __('as the other');
        __('even mixing goes');
        __n('like this', 'or that', 3);
        __('duplicate strings are filtered');
        __('duplicate strings are filtered');
        __('duplicate strings are filtered');
        __("Oh, and by the way: don't fail on this!");
        __('Oh, and by the way: this don\'t fly either!');
        __n('What\'s in a count', 'Won\'t you count it', 3);
        __('Accept (parenthesis) in a string');
        __n("we can have some (parenthesis)", "we can have one (parenthesis)", 3);
        __('a "string" can have quotes');
        __("a 'string' can have quotes");
        __("a string can \" mix a quote in various ways");
    let strings = scan([__filename]);


The tool xgettext has good support for javascript, and extracts strings faster then this tool, but is hardcoced for the _('literal') notation.

Some other good tools exist, such as:

roadmap and future development

Please drop me a personal note on matthijsatischendotnl if you want to use this and I need to fix things.