Mako InternationalizationΒΆ


This recipe is extracted, with permission, from a blog post made by Alexandre Bourget.

First, add subscribers within your Pyramid project's

def main(...):
    # ...

Then add, a module to your project's package directory.


from pyramid.i18n import get_localizer, TranslationStringFactory

def add_renderer_globals(event):
    # ...
    request = event['request']
    event['_'] = request.translate
    event['localizer'] = request.localizer

tsf = TranslationStringFactory('YOUR_GETTEXT_DOMAIN')

def add_localizer(event):
    request = event.request
    localizer = get_localizer(request)
    def auto_translate(*args, **kwargs):
        return localizer.translate(tsf(*args, **kwargs))
    request.localizer = localizer
    request.translate = auto_translate

After this has been done, the next time you start your application, in your Mako template, you'll be able to use the simple ${_(u"Translate this string please")} without having to use get_localizer explicitly, as its functionality will be enclosed in the _ function, which will be exposed as a top-level template name. localizer will also be available for plural forms and fancy stuff.

This will also allow you to use translation in your view code, using something like:

def my_view(request):
    _ = request.translate
    request.session.flash(_("Welcome home"))

For all that to work, you'll need to:

(env)$ easy_install Babel

And you'll also need to run these commands in your project's directory:

(env)$ python extract_messages
(env)$ python init_catalog -l en
(env)$ python init_catalog -l fr
(env)$ python init_catalog -l es
(env)$ python init_catalog -l it
(env)$ python update_catalog
(env)$ python compile_catalog

Repeat the init_catalog step for each of the langauges you need.


The gettext sub-directory of your project is locale/ in Pyramid, and not i18n/ as it was in Pylons. You'll notice that in the default setup.cfg of a Pyramid project.

At this point you'll also need to add your local directory to your project's configuration.

def main(...):
   # ...

Lastly, you'll want to have your Mako files extracted when you run extract_messages, so add these to your (yes, you read me right, in so that Babel can use it when invoking it's commands).

    # ...
        # ...
        # ...
    message_extractors = {'yourpackage': [
            ('**.py', 'python', None),
            ('templates/**.html', 'mako', None),
            ('templates/**.mako', 'mako', None),
            ('static/**', 'ignore', None)]},
    # ...

In the above triples the last element, None in this snippet, may be used to pass an options dictionary to the specified extractor. For instance, you may need to set Mako input encoding using the corresponding option.

# ...
           ('templates/**.mako', 'mako', {'input_encoding': 'utf-8'}),
# ...