Django multi-language (i18n) and SEO

I am developing a multilingual site in Django.

To improve SEO, I will give each language version a unique URL, as shown 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 the django_language key in a user session or cookie to determine the default language. Thus, no matter what language the user chooses, the URL is always the same. For example: http://www.foo.com/index.html

How to solve this problem?

+4
source share
2 answers

django CMS has the feature you are looking for. It looks like you are looking for a CMS, so this might be useful.

If you want to do this manually, you should take a look at urls.py

+1
source

We did this by running a piece of middleware to activate the desired language by analyzing it from the request URL.

Something like that:

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() 
+1
source

Source: https://habr.com/ru/post/1307553/


All Articles