Extracting Javascript gettext messages using Babel CLI extractor

2024/10/2 1:27:09

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

Babel comes with a few builtin extractors: python (which extractsmessages from Python source files), javascript, and ignore (whichextracts 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 <[email protected]>\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.

Answer

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
https://en.xdnf.cn/q/70904.html

Related Q&A

Getting TTFB (time till first byte) for an HTTP Request

Here is a python script that loads a url and captures response time:import urllib2 import timeopener = urllib2.build_opener() request = urllib2.Request(http://example.com)start = time.time() resp = ope…

accessing kubernetes python api through a pod

so I need to connect to the python kubernetes client through a pod. Ive been trying to use config.load_incluster_config(), basically following the example from here. However its throwing these errors. …

Understanding DictVectorizer in scikit-learn?

Im exploring the different feature extraction classes that scikit-learn provides. Reading the documentation I did not understand very well what DictVectorizer can be used for? Other questions come to …

Parsing RSS with Elementtree in Python

How do you search for namespace-specific tags in XML using Elementtree in Python?I have an XML/RSS document like:<?xml version="1.0" encoding="UTF-8"?> <rss version=&quo…

String module object has no attribute join

So, I want to create a user text input box in Pygame, and I was told to look at a class module called inputbox. So I downloaded inputbox.py and imported into my main game file. I then ran a function in…

TypeError: the JSON object must be str, not Response with Python 3.4

Im getting this error and I cant figure out what the problem is:Traceback (most recent call last):File "C:/Python34/Scripts/ddg.py", line 8, in <module>data = json.loads(r)File "C:…

Redirect while passing message in django

Im trying to run a redirect after I check to see if the user_settings exist for a user (if they dont exist - the user is taken to the form to input and save them).I want to redirect the user to the app…

Django sorting by date(day)

I want to sort models by day first and then by score, meaning Id like to see the the highest scoring Articles in each day. class Article(models.Model):date_modified = models.DateTimeField(blank=True, n…

ImportError: No module named pynotify. While the module is installed

So this error keeps coming back.Everytime I try to tun the script it returns saying:Traceback (most recent call last):File "cli.py", line 11, in <module>import pynotify ImportError: No …

business logic in Django

Id like to know where to put code that doesnt belong to a view, I mean, the logic.Ive been reading a few similar posts, but couldnt arrive to a conclusion. What I could understand is:A View is like a …