Skip to content Skip to sidebar Skip to footer

How To Make Django Support Ietf Language Tag (xx-yy Format)?

We have a Django site that supports many languages. Trying to add opensearch plug-ins support for multi-language. OpenSearch.org spec uses IETF language tag (xx-YY format). Default

Solution 1:

Well as far I can test, Django i18n does supports fall-back xx-YY to xx then to default (en in my case) but only for Accept-Language user agent header. It does not do same for URL language switch.

Here is the solution I could come up with:

from django.views.generic import RedirectView
from django.conf import settings
...
urlpatterns += patterns('',
  url(r'^(?P<lang>[a-z]{2})-[A-Za-z]{2}/(?P<path>.*)$', RedirectView.as_view(url='/%(lang)s/%(path)s',query_string=True)),
  url(r'^[a-z]{2}/(?P<path>.*)$', RedirectView.as_view(url='/{}/%(path)s'.format(settings.LANGUAGE_CODE),query_string=True)),
)
  • Any xx-YY not handled by i18n pattern redirected to xx
  • Any xx not handled by i18n pattern redirected to default language set using LANGUAGE_CODE.

Solution 2:

You need to specify the supported languages in your settings:

LANGUAGES = (
    ('en', _('English'),
    ('en-gb', _('British English'),
    ('en-au', _('Australian English'),
    ('es', _('Spanish'),
    ('es-ar', _('Argentinian Spanish'),
)

From there, use i18n_urlpatterns. When you create PO files, you will need to run python manage.py makemessages -l en_GB, etc. Check here for more information.

Post a Comment for "How To Make Django Support Ietf Language Tag (xx-yy Format)?"