1

I'm looking for a way to detect user language. I set my default language as 'en_US', and I translated my site for 'pt_BR'. How can I switch the language and show 'pt_BR' version for Brazilians and 'en_US' for the rest of the world?

I read these documentation links:

https://docs.djangoproject.com/en/dev/topics/http/sessions/

https://docs.djangoproject.com/en/1.7/topics/i18n/translation/

I believe that maybe I'll have to take this information from user cookies or browser preferences, but how can I do this?

Furthermore, how can I test different languages? I have to change my browser language? OS language? Use a proxy?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Alexandre Lara
  • 2,464
  • 1
  • 28
  • 35

3 Answers3

3

Every user's HTTP request contains in header parameter Accept-Language.

Example would be:

user_langs = request.META.get('HTTP_ACCEPT_LANGUAGE', ['en-US', ])
gcerar
  • 920
  • 1
  • 13
  • 24
  • Is there a middleware or settings option to apply this header to the response language automatically? – Mohammad Jafar Mashhadi Aug 22 '17 at 07:28
  • Let's say that my answer is obsolete. Django has introduced ``LocaleMiddleware``, which dynamically determines the proper language of a user. Notice, that there are always some edge cases. For further information please refer to [Django's i18n page](https://docs.djangoproject.com/en/1.11/topics/i18n/) and [How Django discovers language preference](https://docs.djangoproject.com/en/1.11/topics/i18n/translation/#how-django-discovers-language-preference) – gcerar Aug 22 '17 at 08:28
1

Try to add navigator.language to your post data and resolve it in your view.

http://www.w3schools.com/jsref/prop_nav_language.asp

obayhan
  • 1,636
  • 18
  • 35
0

Django detects which language it should use based on the browser settings. So changing your browser language will allow you to test.

If you set 'en_US' as default language, it will work. For more information, see: https://docs.djangoproject.com/en/4.2/topics/i18n/translation/#how-django-discovers-language-preference

Wim Feijen
  • 788
  • 7
  • 9