L10n:Web frameworks
So, you're using a new web framework or programming language and want to make it localizable...
Really?
First up, stop. No. Please use Playdoh for the frontend portions of your code, it already is wired up to localize HTML and JavaScript sent to the browser. It and the libraries it ships with are all coded against the quirky and nuanced way our l10n community prefers things to be done.
What goes into a framework
Okay, for whatever reason you really need to do L10n the Mozilla way, this page will disect Playdoh to capture the scope of implementing i18n/l10n on a web framework.
Translate Toolkit
The Translate Toolkit is a Python package that assists in localization of software.
It provides many CLI like:
- po2html
- php2po
- moz2po
- po2moz
On php applications, we use straight gettext command line tools. Python's syntax and library support for gettext makes this impossible, so translate is a way to do gettext like work-flows on code.
Babel
A collection of tools for internationalizing Python applications.
Primarily Babel provides l10n to jinja. Jinja is the foundation of the template library jingo.
Tower
Tower fills in Mozilla specific wholes for Babel.
- Pulls strings from a variety of sources: Python, JavaScript, and .lhtml files.
- Collapses whitespace in all strings to prevent unwieldy msgids.
- Supports Gettext context (msgctxt) in all gettext, and ngettext calls.
FunFactory
FunFactory (webdev library) provides a LocaleURLMiddleware which does locale detection and redirects. Again this bakes in the Mozilla specific way to do these things and accounts for user-agent bugs, etc.
It's configured to use the local codes in the same case as is preferred by our L10n community.
It also provides template helper functions which will format a URL with the locale code at the begging, which is our standard. It updates Django's url de-referencing bit so you have reverse a short name for a View into the locale aware URL.
Django/Jinja/Jingo
The Django web framework provides many i18n features. We use Jinja2 and Jingo in the Playdoh framework.
Django/jinja provides ugettext and ugettext_lazy. These are typically aliased to _ and _lazy and used in Python code (Models, Controllers, and business logic).
There are several template tags: _, ngettext, and trans. These have been tweaked (f and fe filters) based on feedback from security to make their security reviews easier.
trans tag
The trans tag can be used in templates to localize large chunks of text.