Personal tools
You are here: Home Plone XP leocornus.django.ploneproxy Internationalization and Localization for Django Application

Internationalization and Localization for Django Application

How to support i18n and i10n for a Django Application?

This is required for all templates!

  • login form template!
  • forget password wizard template!
  • reset password wizard template!

PloneProxy LocaleMiddleware

leocornus.django.ploneproxy.middleware.LocaleMiddleware

adding a language parameter in request.  The value of the language parameter will be the No. 1 choice for the active language.

introduce a setting for the parameter name PLONEPROXY_LANG_PARAM_NAME, default value is currently hard coded as ldp_lang. (TODO: we shall change this to better way! there should be some way to set the default value for a new introduce setting)

  • the login view class will check the active language from django.utils.translation.getLanguage()
    - request.LANGUAGE_CODE might be easier!  Since we will set it when in the request phase middleware.
  • the login template will prepare the link based on the active language, set the language parameter to the other language
  • login template will render with the link to switch to another language.
  • the middle will set the active language based on the value of language parameter first!
    - if there is no language parameter then using the logic from default LocaleMiddleware to find the active language!
  • set the request.LANGUAGE_CODE just as what default LocaleMiddleware did.

Django i18n Support

using standard Python module gettext

There are 2 useful Django manage commands: makemessages and compilemessages

Middleware

LocalMiddleware (django.middleware.locale.LocaleMiddleware) is required for allowing user to select his / her prefered language and must be after SessionMiddleare.  The language discovery sequence is like following:

  • django_language key in current user's session
  • language setting in cookie: cookie is set by LANGUAGE_COOKIE_NAME, default is django_language.
  • The Accept-Language HTTP header
  • the global LANGUAGE_CODE setting, default is 'en-us'

Settings

Django settings for i18n, http://docs.djangoproject.com/en/dev/ref/settings/#setting-LANGUAGE_CODE

  • LANGUAGE_CODE is used to set up the language for installation, default is 'en-us'
  • LANGUAGES to set a tuple of all available languages
  • LANGUAGE_COOKIE_NAME = 'django_language'
  • LOCAL_PATHS

i18n Message Files

A message file is a plain text file, which has a .po file extension.  Every single language should have corresponding message file.  Django provides a manage command makemessages to help you make a message file easily.  For example, the following command will make an English message file.

$ python manage.py makemessages -a -l en

The message file will located in folder $PROJECT_HOME/locale.  And we have to create the locale folder manually,  The command makemessages will not create this folder automatically!

After create the message files, we need compile the message files so Django could pick it up at runtime.  Django provides a manage command for this too:

$ python manage.py compilemessages

References

Tracking History

When Who What Done
2010-05-20 09:41 Sean Chen we now have a hidden field for this and the settings' name changed to PLONEPROXY_LANG_FIELD_NAME. revision: r418 and r419
-- 1.0 Hours, 100.0% Done
2010-05-19 09:21 Sean Chen create a locale middleware to set language based on a language parameter on Request. A setting called PLONEPROXY_LANG_PARAM_NAME is introduced to set the lang parameter's name. revision: r414
-- 2.0 Hours, 100.0% Done
2010-05-19 06:50 Sean Chen adding English and French support for login form, using some dummy testing data. revision: r413
-- 2.0 Hours, 80.0% Done
Document Actions