Kay has a almost full stack i18n feature including extracting messages from the source, adding new language to your app, updating and compiling the message catalogs and a handlers for javascript gettext implementation. This feature is basicaly based on the jinja2’s i18n feature, so it is a good idea to refer to jinja2’s documentation hosted at:
http://jinja.pocoo.org/2/documentation/
Kay automatically detects user’s language by parsing Accept-Language header sent by user’s browser. You can also create a link for setting preffered language in user’s cookie which overrides Accept-Language setting.
You can mark your messages for translation in python code as following:
from kay.i18n import gettext as _
_('Hello')
You can mark your messages for translation in templates as following:
<p>{{ _('Hello') }}</p>
<p>{% trans %}Hello{% endtrans %}
You can extract the messages from myapp application and create the template catalog file as following:
$ python manage.py extract_messages myapp
The extract_messages subcommand accepts optional --domain parameter. The value for this parameter must be messages or jsmessages.
You can add new Japanese translations from the template like:
$ python manage.py add_translations myapp -l ja
You can update your translations with updated translation template as following:
$ python manage.py update_translations myapp -l ja
You can compile all of your translations under myapp application as following:
$ python manage.py compile_translations myapp
There is a function for creating a URL that will set preffered language in user’s cookie. To use this capability, you need to add kay.i18n to INSTALLED_APPS attribute as follows:
INSTALLED_APPS = (
'kay.i18n',
)
In jinja2 context, create_lang_url function is automatically loaded. So you can use this function for creating a link for setting language explicitly as follows:
<a href="{{ create_lang_url(lang='en') }}">{{ _('English') }}</a>
<a href="{{ create_lang_url(lang='ja') }}">{{ _('Japanese') }}</a>
You can define a handler for pseudo javascript gettext as following:
from kay.views.i18n import javascript_catalog
return Map([
Rule('/_i18n.js', endpoint='i18n_js',
defaults={'packages':('myapp','kay')}),
])
all_views = {
'i18n_js': javascript_catalog,
}