Extract javascript gettext messages using Babel CLI extractor

It states that Babel can retrieve gettext messages for Python and Javascript files.

Babel has several built-in extractors: python (which extracts messages from Python source files), javascript and ignore (doesn't extract anything).

The output from the command line is documented here - but without usage examples.

Also in the same index above, there is a mention of the configuration file that will be used with the extraction, but not much expanded.

When I run the main command for the extractor in the directory with js files, I get only the .PO header and not the message.

$ pybabel extract   /path/to/js-dir

# Translations template for PROJECT.
# Copyright (C) 2012 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2012.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2012-04-22 19:39+1000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 0.9.6\n"

$ 

Here is an example segment from a js file that I am trying to extract from messages:

else if(data.status == "1"){
    var follow_html = gettext('Follow');
    object.attr("class", 'button follow');
    object.html(follow_html);
    var fav = getFavoriteNumber();
    fav.removeClass("my-favorite-number");
    if(data.count === 0){
        data.count = '';
        fav.text('');
    }else{
        var fmts = ngettext('%s follower', '%s followers', data.count);
        fav.text(interpolate(fmts, [data.count]));
    }
}

, - CLI , , .

+5
4

(babel.cfg) :

[javascript:*.js]
encoding = utf-8

do:

pybabel extract -F babel.cfg /path/to/js-dir

, .

, extract, :

pybabel extract --help
+6

, , babel.

pybabel extract -k __ -F babel.cfg --no-default-keywords /path/to/js-dir 

(-k [keyword]). -k __, "__" , .

"__" -k babel.cfg.

: , gettext()

+3

global gettext

g.i18n = {
    'Casa' : lazy_gettext('Home'),
    'Auto' : lazy_gettext('Car'),
    'Persona' : lazy_gettext('Person')
}

<script>
    var i18n = {{ g.i18n | tojson }}
</script>

JS:

var labelTranslate = {
                    Casa: i18n.Casa,
                    Persona: i18n.Persona,
                    Auto: i18n.Auto
                };
0

gettext Javascript.

See: jsgettext . It allows the use of standard * gettext functions, including contexts and / or multiple forms.

It can read PO / MO files or you can import generated JSON files.

See this file for this project for a complete example.

0
source

All Articles