22

I have a django 1.6 site with i18n working. I can change the frontend language with a select box in the top of the template, but I don't know if there is a django app or trick to change the admin language, because it seems to store somewhere in session variable, and it keeps the first language I have used in the frontend.

maikelpac
  • 303
  • 1
  • 3
  • 7

4 Answers4

33

In your settings.py just add 'django.middleware.locale.LocaleMiddleware' to your MIDDLEWARE_CLASSES setting, making sure it appears after 'django.contrib.sessions.middleware.SessionMiddleware'.

Jose Ricardo Bustos M.
  • 8,016
  • 6
  • 40
  • 62
Edditado
  • 331
  • 3
  • 2
  • 1
    I wonder, why it was downvoted. That's exactly what I was missing. – x-yuri Nov 22 '16 at 08:33
  • Nice answer, it's easy and it works, but how can an user change the language? Depending on the language set in browser is not always a good solution. – Smit Johnth Mar 24 '17 at 17:52
  • Adding .locale.LocaleMiddleware is the only one thing which is necessary. Then admin will be in the language which is most preferred in the users browser setting. – mirek Dec 22 '17 at 09:05
  • of course for model entries inside admin you need more; you need use gettext_lazy() in model.py for such entries (or ugettext_lazy prior to dj2.0), then create .po file (./manage.py makemessages -l de_DE), translate it with poedit, compile the .po file (./manage.py compilemessages) – mirek Dec 22 '17 at 16:13
20

You can create /en/admin, /fr/admin/ and so on using i18n_patterns:

urlpatterns += i18n_patterns(
    url(r'^admin/', include(admin.site.urls)),
)

(For Django <= 1.7, you must specify a prefix, use i18n_patterns('', ... ))

Udi
  • 29,222
  • 9
  • 96
  • 129
  • 1
    it changes my application language correctly. but it dosent change the language file i also provided. how can i load that language ? can you help me ? [my question](http://stackoverflow.com/questions/39102538/language-file-dosent-load-automaticaly-in-django) @Udi – Mehrdad Pedramfar Aug 23 '16 at 14:15
14

Here is a slightly modified version of a code snippet from the Django docs for admin/base.html that adds a language selection dropdown:

{% extends "admin/base.html" %}
{% load i18n %}
{% block userlinks %}
{{ block.super }}
/ <form action="{% url 'set_language' %}" method="post" style="display:inline">{% csrf_token %}
    <input name="next" type="hidden" value="{{ redirect_to }}">
    <select name="language" onchange="this.form.submit()">
        {% get_current_language as LANGUAGE_CODE %}
        {% get_available_languages as LANGUAGES %}
        {% get_language_info_list for LANGUAGES as languages %}
        {% for language in languages %}
            <option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
                {{ language.name_local }} ({{ language.code }})
            </option>
        {% endfor %}
    </select>
</form>
{% endblock %}

For this to work you also need to add the following to your urlpatterns:

path('i18n/', include('django.conf.urls.i18n')),
Ahmed Shehab
  • 1,657
  • 15
  • 24
Eerik Sven Puudist
  • 2,098
  • 2
  • 23
  • 42
  • 1
    This solution is very useful if you want to see changing languages and translation results. I needed to add `django.middleware.locale.LocaleMiddleware` after `SessionMiddleware` and I have added code above to (locally created app and that app put before `admin`) `templates/admin/base_site.html` – WBAR Jan 01 '20 at 01:35
  • 1
    The code snippet is missing the following at the top: `{% extends "admin/base.html" %} {% load i18n %}` – Vlax Jan 18 '20 at 17:20
0

Change your browser default language.

Instead of spoiling the code. E.g.,
Chrome -> Settings -> Language -> Preferred languages: move upper Language you need.

Refresh browser, and the admin panel will change the displayed language.

BRap
  • 529
  • 2
  • 10
  • 29