2

request.LANGUAGE_CODE tell me what language the user has selected. such as en for English or de for German.

But how do I get the culture code? Such as GB for British English and us for American English?

Many Thanks,

Houman
  • 64,245
  • 87
  • 278
  • 460

2 Answers2

2

There's request.META['HTTP_ACCEPT_LANGUAGE']. However, this is set by client (web browser, typically), so it may not be set or set properly in some cases. The only fool-proof method is to ask the user to select their preferred language, and then use that setting. However, if it's not crucial that the site be in the right language and you want it to "just work" for the user without intervention, then using the HTTP_ACCEPT_LANGUAGE header is fine. You can even combine the two approaches so the user can switch if they'd prefer a different language than what was detected.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
1

The Django i18n context processor ("django.core.context_processors.i18n") adds your LANGUAGES setting to your context. Which let you do stuff like:

<form action="{% url 'set_language' %}" method="post">
{% csrf_token %}
<input name="next" type="hidden" value="{{ redirect_to }}" />
<select name="language">
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<option value="{{ language.code }}">{{ language.name_local }} ({{ language.code }})</option>
{% endfor %}
</select>
<input type="submit" value="Go" />
</form>

https://docs.djangoproject.com/en/dev/topics/i18n/translation/#django.conf.urls.i18n.set_language

It's also valid to add dialect, e.g. have a look at this post:

Django: How to add Chinese support to the application

Community
  • 1
  • 1
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
  • Thanks for that. Initially I was trying apply the language and culture settings according to their browser HTTP_ACCEPTED_LANGUAGE settings. But this seems somehow flaky. Maybe I should allow the user to save their favorite language in the database and acquire it each time from there. Let me have a play first... :) – Houman Aug 13 '12 at 19:43