views:

202

answers:

2

I am developing a multi-language site in Django.

In order to improve SEO, I will give every language version a unique URL like below,

  • english: www.foo.com/en/index.html
  • french: www.foo.com/fr/index.html
  • chinese: www.foo.com/zh/index.html

However,

Django looks for a "django_language" key in user's session or cookie to determine language in default. So, despite which language the user chose, the URL is always the same. For instance: http://www.foo.com/index.html

How to resolve this problem?

A: 

django CMS has the feature you're looking for. Looks like you're looking for a CMS, so it can be useful.

If you want to do it by hand, you should take a look at urls.py

DZPM
DZPM:django CMS is a good sample,thank you
fumer
A: 

We've done this by implementing a piece of middleware to activate the desired language by parsing it from the request url.

Something like this:

class LanguageInPathMiddleware(object):
    def __init__(self):
        self.language_codes = set(dict(settings.LANGUAGES).keys())

    def process_request(self, request):
        language_code = request.path_info.lstrip('/').split('/', 1)[0]
        if language_code in self.language_codes:
            translation.activate(language_code)
            request.LANGUAGE_CODE = translation.get_language()
notanumber
to: notanumberi found a good django app "django-loacleurl". thank you
fumer