Skip to content

Translations and Extensions

Adrià Mercader edited this page Jan 18, 2016 · 9 revisions

DEPRECATED: To translate extensions on CKAN >= 2.5, please check http://docs.ckan.org/en/latest/extensions/translating-extensions.html


Extracting Strings

Create a file called babel.cfg in the top level of your extension with the following contents:

# Extraction from Python source files
[python: **.py]
# Extraction from Genshi HTML and text templates
[jinja2: **.html]
keywords = _
encoding = utf-8
extensions =

Now run create an i18n folder to store the translations:

mkdir i18n

Now extract the strings with the following command:

python setup.py extract_messages --mapping-file babel.cfg --output i18n/ckanext.pot

If you use any jinja2 statements that are not part of core, you need to include it under extensions in babel.cfg or it will fail without any warning. The extraction process will skip that file completely but not explicitly show the failure. Remember to also include the core Jinja2 extensions if the templates in your extension uses them. CKAN extensions can be found in ckan/lib/jinja_extensions.py

For example, if a template in your extension uses {% resource %} and {% with %}, add the following to the extensions line:

extensions = ckan.lib.jinja_extensions.ResourceExtension,jinja2.ext.with_

Initializing Translations

The extracted ckanext.pot file can now be initialized for a language (Replace fr with locale of your choice):

python setup.py init_catalog -l es -i i18n/ckanext.pot \
-o i18n/es/LC_MESSAGES/ckanext.po

Merging Translations

First set your i18n.directory to point to a new directory to store your merged translations:

ckan.i18n_directory = /opt/locales/i18n/

For merging translations, use the msgcat command for each language you need to merge:

msgcat --use-first \
    "i18n/fr/LC_MESSAGES/ckanext.po" \
    "/usr/lib/ckan/default/src/ckan/ckan/i18n/fr/LC_MESSAGES/ckan.po" \
    | msgfmt - -o "/opt/locales/i18n/fr/LC_MESSAGES/ckan.mo"
Clone this wiki locally