Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting Javascript gettext messages using Babel CLI extractor

It is stated here that Babel can extract gettext messages for Python and Javascript files.

Babel comes with a few builtin extractors: python (which extracts messages from Python source files), javascript, and ignore (which extracts nothing).

The command line extractor is documented here - but with no examples on usage.

Also in the same pointer above, there is some mention of a config file to be used with extraction, but not much expanded on.

When I run the basic command for the extractor on a dir with js files, I get only the .PO header generated but no messages.

$ 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 a sample segment from a js file I'm trying to extract messages for:

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]));
    }
}

I would appreciate it if someone can provide exact CLI options and config settings to make the extraction work, or a pointer to such.

like image 287
Basel Shishani Avatar asked Apr 22 '12 10:04

Basel Shishani


2 Answers

Create a file (babel.cfg) with the following content:

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

Then, do:

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

That should be enough for you to have some message strings.

BTW, you can consult the help for the extract command by doing:

pybabel extract --help
like image 54
ubik Avatar answered Nov 16 '22 14:11

ubik


I had a similar issue and was able to get around it by disabling default keywords with babel.

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

You must specify at least one keyword in the command when you disable the defaults (-k [keyword]). I chose -k __ because "__" was a pattern I was looking for.

Just use this command and replace the "__" after -k with one from your babel.cfg file.

Edit: this allows you to use your own keywords rather than gettext()

like image 32
AssHat_ Avatar answered Nov 16 '22 13:11

AssHat_