localizing_using_jinja2.rst 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. -----------------------------
  2. Localizing themes with Jinja2
  3. -----------------------------
  4. 1. Localize templates
  5. ---------------------
  6. To enable the |ext| extension in your templates, you must add it to
  7. *JINJA_EXTENSIONS* in your Pelican configuration::
  8. JINJA_EXTENSIONS = ['jinja2.ext.i18n', ...]
  9. Then follow the `Jinja2 templating documentation for the I18N plugin <http://jinja.pocoo.org/docs/templates/#i18n>`_ to make your templates localizable. To enable `newstyle gettext calls <http://jinja.pocoo.org/docs/extensions/#newstyle-gettext>`_ the *I18N_GETTEXT_NEWSTYLE* config variable must be set to ``True`` (default).
  10. .. |ext| replace:: ``jinja2.ext.i18n``
  11. 2. Specify translations location
  12. --------------------------------
  13. The |ext| extension uses the `Python gettext library <http://docs.python.org/library/gettext.html>`_ for translating strings.
  14. In your Pelican config you can give the path in which to look for translations in the *I18N_GETTEXT_LOCALEDIR* variable. If not given, it is assumed to be the ``translations`` subfolder in the top folder of the theme specified by *THEME*.
  15. The domain of the translations (the name of each translation file is ``domain.mo``) is controlled by the *I18N_GETTEXT_DOMAIN* config variable (defaults to ``messages``).
  16. Example
  17. .......
  18. With the following in your Pelican settings file::
  19. I18N_GETTEXT_LOCALEDIR = 'some/path/'
  20. I18N_GETTEXT_DOMAIN = 'my_domain'
  21. … the translation for language 'cz' will be expected to be in ``some/path/cz/LC_MESSAGES/my_domain.mo``
  22. 3. Extract translatable strings and translate them
  23. --------------------------------------------------
  24. There are many ways to extract translatable strings and create ``gettext`` compatible translations. You can create the ``*.mo`` files yourself, or you can use some helper tool as described in `the Python gettext library tutorial <http://docs.python.org/library/gettext.html#internationalizing-your-programs-and-modules>`_.
  25. Recommended tool: babel
  26. .......................
  27. `Babel <http://babel.pocoo.org/>`_ makes it easy to extract translatable strings from the localized Jinja2 templates and assists with creating translations as documented in this `Jinja2-Babel tutorial <http://pythonhosted.org/Flask-Babel/#translating-applications>`_ [#flask]_.
  28. .. [#flask] Although the tutorial is focused on Flask-based web applications, the linked translation tutorial is not Flask-specific.