Introducing babel-plugin-django-gettext.
Review Request #10993 — Created April 8, 2020 and submitted — Latest diff uploaded
This is a plugin for Babel designed to make it easier to perform
localization in a Django-backed JavaScript codebase.When working with localization, Django provides a handful of
gettext-compatible functions and a version of the current message
dictionary for the locale, which JavaScript can use to perform
on-the-fly translation of strings. While fantastic to have, it suffers
from a lot of noise, caused by long strings that can't easily be
word-wrapped and wordyinterpolate()
calls for format strings.This plugin fixes all this by introducing the following:
-
Automatic whitespace condensing/trimming (by default -- callers can
also opt to preserve all whitespace by calling_raw()
versions of
the functions). -
Automatic interpolation for template literals that reference variables
or expressions, avoiding all theinterpolate()
calls. -
Support for either calling gettext functions or using them as a
tagged template literal. -
A
_
alias that maps togettext
when used as a tagged template
literal (but not a function call, to avoid breaking usage of
underscore.js). -
A
N_
aliases that maps tongettext
.
These calls/template literals will be transpiled cleanly to code
compatible with the xgettext
message scanner, resulting in cleaner
codebases, more readable strings, and fewer errors.
Unit tests passed.
Tested this with Djblets and Review Board, and verified the resulting
output.