50๐
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'
...
22๐
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'
- [Django]-ValueError: Unable to configure handler 'file': [Errno 2] No such file or directory:
- [Django]-How can I make a Django model read-only?
- [Django]-How to show related objects in Django/Admin?
2๐
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 the
LANGUAGE_COOKIE_NAME
setting.)
- [Django]-Django and Restful APIs
- [Django]-AttributeError: module 'html.parser' has no attribute 'HTMLParseError'
- [Django]-How to set up custom middleware in Django?
2๐
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>
- [Django]-Django โ No module named _sqlite3
- [Django]-Limiting Memory Use in a *Large* Django QuerySet
- [Django]-Adding a jQuery script to the Django admin interface