How to get the current language in Django?
Asked Answered
I

7

117

How can I get the current language in Django?

Inferno answered 28/7, 2010 at 20:1 Comment(1)
Please be specific. 'The language of my web' - do you mean the web request, the web server, or what?Hubey
S
174

Functions of particular interest are django.utils.translation.get_language() which returns the language used in the current thread. See documentation.

Spiros answered 28/7, 2010 at 20:19 Comment(1)
Caveat: Returns None if translations are temporarily deactivated (by deactivate_all() or when None is passed to override()). Before Django 1.8, get_language() always returned LANGUAGE_CODE when translations were deactivated.Basalt
S
112

Or you can also get this in your views

request.LANGUAGE_CODE
Septa answered 29/7, 2010 at 6:11 Comment(6)
I voted this up (from -1 for some reason). Note the following (from docs.djangoproject.com/en/dev/topics/i18n/deployment/… "with static (middleware-less) translation, the language is in settings.LANGUAGE_CODE, while with dynamic (middleware) translation, it's in request.LANGUAGE_CODE."Stenson
That link is dead, and I see no reason for not using the documented accepted solution above: django.utils.translation.get_language()Sourpuss
trying to get the language in e.g. models would not be possible if there is no request yet. I think the django.utils.translation.get_language() is always a better solution.Earthaearthborn
What about to get the current language in a template?Gilges
@Gilges this might be useful for you docs.djangoproject.com/en/dev/topics/i18n/translation/…Delectable
Thanks been googlin this for 20 minutes. For template use {{ request.LANGUAGE_CODE }}Station
F
48

Be careful of the method you use to get the language. Depending on which method, Django will use different ways and informations to determine the right language to use.

When using the django.utils.translation.get_language() function, it's linked to the thread language. Before Django 1.8, it always returned settings.LANGUAGE_CODE when translations were disabled. If you want to manually override the thread language, you can use the override() or activate() functions, which is not very explicitly named, but well, still useful:

from django.utils import translation

with translation.override('fr'):
    print(_("Hello")) # <= will be translated inside the with block

translation.activate('fr') # <= will change the language for the whole thread.
# You then have to manually "restore" the language with another activate()
translation.activate('en') # <= change languages manually

If you want django to check the path and/or request (language cookie, ...), which is a lot more common e.g. www.example.com/en/<somepath> vs www.example.com/fr/<somepath>, use django.utils.translation.get_language_from_request(request, check_path=False). Also, it will always return a valid language set in settings.LANGUAGES

I found it not very easy to find these differences through Google about this subject so here it is for further reference.

Fluorite answered 11/11, 2015 at 11:4 Comment(2)
Note that it is django.utils.translation, not translations. There is a misspelling in the link provided. In the snippet it is correct.Lanti
+1 for the difference between django.utils.translation.get_language() and django.utils.translation.get_language_from_request(request, check_path). If in view, you should use the latter with check_path = True to get the language your template will get rendered in.Maffa
H
11

Just to add that if you do use django.utils.translation.get_language() then you should bear in mind that if that section of code will be called asynchronously (e.g. as a celery task) then this approach won't work due to it running in a different thread.

Haller answered 30/10, 2013 at 11:5 Comment(1)
The obvious approach here would be to pass language as task parameter, and then set language with translation.activate(language)Eveliaevelin
B
6

You can use these template tags in Django's templating language:

{% load i18n %}

{% get_current_language as LANGUAGE_CODE %}

Current language code: {{ LANGUAGE_CODE }}<br>

{% get_current_language_bidi as LANGUAGE_BIDI %}

{% if LANGUAGE_BIDI %}RTL <br>{% endif %}

{% get_language_info for LANGUAGE_CODE as lang %}

Language code: {{ lang.code }}<br>
Name of language: {{ lang.name_local }}<br>
Name in English: {{ lang.name }}<br>
Bi-directional: {{ lang.bidi }}
Name in the active language: {{ lang.name_translated }}
Beset answered 2/12, 2021 at 20:36 Comment(0)
P
1

You can get the current language with LANGUAGE_CODE and get_language() in Django Views as shown below:

# "views.py"

from django.shortcuts import render
from django.utils.translation import get_language

def test(request):
    print(request.LANGUAGE_CODE) # en
    print(get_language()) # en
    return render(request, 'index.html')

And, you can get the current language with LANGUAGE_CODE and get_current_language in Django Templates as shown below. *get_current_language needs to load i18n:

{% "index.html" %}

{% load i18n %}

{{ request.LANGUAGE_CODE }} {% "en" %}

{% get_current_language as current_language %}
{{ current_language }} {% "en" %}
Peeples answered 24/7, 2023 at 10:57 Comment(0)
C
0

You can read the system's locale for language information.

Chevaldefrise answered 28/7, 2010 at 20:4 Comment(1)
You're moderated to -3, but I think the question is vague - "the current language of my web". Not your fault for guessing this means OS.Hubey

© 2022 - 2024 — McMap. All rights reserved.