motatoes
motatoes

Reputation: 926

Django - Suffix all urls with language code

I'm working on a multilingual site so I want to suffix all my urls with the current 2-character language code. If no language is specified I want to default to english.

For example:

mysite.com/ --> mysite.com/en

mysite.com/location --> mysite.com/locations/en

mysite.com/ar will display the arabic site

Since I don't want to add (?P<language>[a-x]{2})$ to all my urls I'm guessing I should write some middleware to check for the suffix and strip it out?

What's the best way to achieve this?

Upvotes: 0

Views: 596

Answers (1)

motatoes
motatoes

Reputation: 926

As indicated in the comment by @Bogdan above I resorted to using the built-in prefixing feature in django.

  • I added the LocaleMiddleware in my MIDDLEWARE_CLASSES setting

  • I added a LANGUAGES setting to specify the languages in my site, in my case it was only English and Arabic so my languages looked like

    LANGUAGES = ( ('ar', _('Arabic')), ('en', _('English')), )

  • In my views I used request.LANGUAGE_CODE to access the language code and display the appropriate language in my template

Upvotes: 1

Related Questions