How to explicitly set django_language in django session
How to explicitly set django_language
in Django
开发者_运维问答 session?
Thanks a lot...
If you want your users to be able to specify language, make sure that LocaleMiddleware
is enabled:
MIDDLEWARE_CLASSES = (
...
'django.middleware.locale.LocaleMiddleware',
...
)
Then Django will look for the user's language preference in that order (see get_language_from_request
in trans_real.py):
- in
request.path_info
, if i18n_patterns are used request.session[settings.LANGUAGE_SESSION_KEY]
(DEPRECATED in Django 3.0, removed in Django 4.0)request.COOKIES[settings.LANGUAGE_COOKIE_NAME]
- every language in
request.META['HTTP_ACCEPT_LANGUAGE']
, until accepted one is found settings.LANGUAGE_CODE
.
As of Django 4.0
The most straightforward way to set language explicitly in Django session is to activate and set the cookie, see the docs:
from django.conf import settings
from django.http import HttpResponse
from django.utils import translation
user_language = 'fr' # example
translation.activate(user_language)
# persist using the cookie
response = HttpResponse(...)
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, user_language)
Before Django 4.0
The most straightforward way to set language explicitly in Django session is to rewrite request.session[settings.LANGUAGE_SESSION_KEY]
:
def someview (request):
...
request.session[settings.LANGUAGE_SESSION_KEY] = 'en'
...
And if you will use a version >= Django 1.8
. Here it is how we could use that:
from django.utils.translation import LANGUAGE_SESSION_KEY
def someview (request):
...
request.session[LANGUAGE_SESSION_KEY] = 'en'
Consider using django.views.i18n.set_language(). Activate this view by adding the following line to your URLconf:
# This example makes the view available at /i18n/setlang/
url(r'^i18n/', include('django.conf.urls.i18n')),
As a convenience, Django comes with a view,
django.views.i18n.set_language()
, that sets a user’s language preference and redirects to a given URL or, by default, back to the previous page.The view expects to be called via the POST method, with a language parameter set in request. If session support is enabled, the view saves the language choice in the user’s session. Otherwise, it saves the language choice in a cookie that is by default named
django_language
. (The name can be changed through theLANGUAGE_COOKIE_NAME
setting.)
I think the best way is to follow Django documentation:
in your urls.py file:
urlpatterns = [
# other libraries
path('i18n/', include('django.conf.urls.i18n')),
]
and in your template file (ex: "/core.html") just call de standard "set_language" view. I'll go for a boostrap switcher:
{% load i18n %}
<ul class="navbar-nav">
<li class="dropdown nav-item">
<a href="#" class="dropdown-toggle nav-link" data-toggle="dropdown">
<i class="nc-icon nc-planet"></i>
</a>
<ul class="dropdown-menu">
<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for lang in languages %}
<button type="submit"
name="language"
value="{{ lang.code }}"
class="dropdown-item">
{{ lang.name_local }}
</button>
{% endfor %}
</form>
</ul>
</li>
</ul>
精彩评论