173👍
Functions of particular interest are django.utils.translation.get_language()
which returns the language used in the current thread. See documentation.
- [Django]-How to produce a 303 Http Response in Django?
- [Django]-Auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'UserManage.groups'
- [Django]-How to override and extend basic Django admin templates?
47👍
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.
- [Django]-'pip' is not recognized as an internal or external command
- [Django]-In Django, how do I check if a user is in a certain group?
- [Django]-Django: TemplateDoesNotExist (rest_framework/api.html)
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.
- [Django]-How do I run tests for all my Django apps only?
- [Django]-Django Model Field Default Based Off Another Field in Same Model
- [Django]-Django: remove a filter condition from a queryset
5👍
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 }}
- [Django]-Python 3 list(dictionary.keys()) raises error. What am I doing wrong?
- [Django]-Django TemplateSyntaxError – 'staticfiles' is not a registered tag library
- [Django]-Cannot set Django to work with smtp.gmail.com
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" %}
- [Django]-Passing variable urlname to url tag in django template
- [Django]-ImportError: Failed to import test module:
- [Django]-Django gunicorn sock file not created by wsgi
- [Django]-How do I filter ForeignKey choices in a Django ModelForm?
- [Django]-Django templates: verbose version of a choice
- [Django]-Django REST Framework custom fields validation