F
F
fovka2020-06-22 23:47:13
Django
fovka, 2020-06-22 23:47:13

How to do internationalization in Django templates?

I want to make a Django site available in a second language.
I register in settings.py

MIDDLEWARE = [
   ...
    'django.middleware.locale.LocaleMiddleware',
]

LANGUAGE_CODE = 'en-us'

LANGUAGES = (
    ('en', 'English'),
    ('nl', 'Dutch'),
)

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)


Next in views.py
from django.utils.translation import ugettext as _
...
context = {
"variable": _("value")
...
}

And in this case, when you run the django-admin makemessages -l nl command, a directory with the django.po file is created in the locale folder and there is my translation context in it. Everything is fine.

But part of the text on the site does not come out of the context, but is physically packed into the template. I'm trying to translate this part by writing in the template:
{% load i18n %}
....

{% trans "My text for translation" %}

And this text from the template, when executing django-admin makemessages -l nl, does not get into django.po. It's like he doesn't exist. Accordingly, there is nowhere to write a translation for it.

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
fovka, 2020-06-24
@fovka

Solved the problem. Deleted the automatic locale folder, then ran django-admin makemessages -l nl not from the folder with manage.py, but from the root directory of the project. Everything is now displayed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question