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,
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.
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: