0

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?

motatoes
  • 828
  • 11
  • 26
  • 2
    Wouldn't it be easier to use the standard way and prefix them with the language code? Django provides a quick way for doing so: https://docs.djangoproject.com/en/1.8/topics/i18n/translation/#module-django.conf.urls.i18n – Bogdan Iulian Bursuc Oct 10 '15 at 07:49
  • @BogdanIulianBursuc Thanks i didnt know this existed! Please add it as an answer and I will accept – motatoes Oct 10 '15 at 21:15

1 Answers1

1

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

motatoes
  • 828
  • 11
  • 26