Back to the overview

gettext, JSX and ES6 template literals

I really like using gettext to translate hardcoded strings into other languages. Django’s translations functionality relies on it as well.

Unfortunately, the xgettext executable which is responsible to collect translatable strings in your code has a bug where it just stops processing files when encountering ES6 template literals inside JSX tags. Support for ES6 template literals was added earlier this year but combining those literals with JSX still doesn’t work.

I wrote a small Python script to extract *gettext calls from JavaScript files; the current version is here. The idea is to find all JavaScript files using git ls-files "*.js", using a regular expression to find *gettext calls and write the output to a place where Django’s ./ makemessages finds it.

I’m certain the code will break too with strange error messages in the near future but it seems to work well, for the moment.

Here’s the current version of the code (hopefully) for your enjoyment:

#!/usr/bin/env python3

import re
import subprocess

def js_files():
    res =
        ["git", "ls-files", "*js", "*mjs"],
    return res.stdout.splitlines()

def gettext_calls(file):
    with open(file, encoding="utf-8") as f:
        return [
            for match in re.findall(

if __name__ == "__main__":
    calls = []
    for file in js_files():

© 2005 - 2020 Matthias Kestenholz