In my settings.py I have the following code:
LANGUAGE_CODE = 'uk'
TIME_ZONE = 'Europe/Kiev'
USE_I18N = True
USE_TZ = True
Now if I open the admin page, I see it translated into Ukrainian. I wanted to display verbose_name of my model fields as translation strings, so I went to the docs and found the instructions. In my model I wrote this:
from django.db import models
from django.utils.translation import gettext_lazy as _
from treenode.models import TreeNodeModel
treenode_display_field = 'name'
name = models.CharField(
I created the ‘locale’ folder in my project root and tried
django-admin makemessages -l uk. It created the .po file where I was able to translate the needed string. But when I opened the admin page, my field name was still in English. In the docs I found that it is needed to add
django.middleware.locale.LocaleMiddleware in the MIDDLEWARE. However, after I add this line my admin page starts to be displayed in Russian. If I delete it, the language gets back to Ukrainian. Any possible reason for this? Thanks!
To assert that everything is working. You also must run the
Thank you for your reply. Yes, this command works
I have checked the language of my browser and it was Russian. After I changed the language of the browser, the language of the admin page also changed. I find it strange that browser language has higher priority than
settings.py. Is this intended behavior?
Yes, absolutely. The intent of the internationalization is to allow the client (browser) to request the language of most value to the person making the request.
What is the reason to set
Also, let’s say I ask someone to add content to the website and give them access to the admin. In their browser they have one language, while I provide translations for another language. This means that they will see some parts of the admin in one language, and other parts in another language.
Perhaps I don’t understand the approach of Django here. Suppose at the moment I want my website (including the admin page) to support only one language, like Ukrainian. Then I can simply use
verbose_name_plural in Ukrainian everywhere - in model names, field named etc. However, my thinking is that one day I may want to add some other languages. So I thought it is better to use translation strings from the start. Then in the future I will not have to edit all my verbose names, it will be enough just to add new languages in the settings and in the
locale folder. Is my line of thought correct?
Also, I recall that I tried to use some third party apps which also used translation strings. And those strings were translated even though I didn’t have
django.middleware.locale.LocaleMiddleware. So perhaps I can use translations even without it? Is this middleware required only in specific situations?
The purpose of LANGUAGE_CODE is defined in the docs for that setting.
The purpose of LocaleMiddleware is defined in its docs.
You probably also want to read How Django discovers language preference.
(I think these all address your questions - feel free to ask further if something isn’t clear in there.)
Thanks, I think this answers my questions:
If you want to let each individual user specify which language they prefer, then you also need to use the
LocaleMiddleware enables language selection based on data from the request. It customizes content for each user.
So now I understand that I don’t need this middleware if I just want to use translation strings, only if I want user’s browser settings to affect the displayed language. Then my only problem is that translation doesn’t work for some reason, but I created a different topic for this.
I appreciate the help, thank you very much!