verbose_name translation for model field is not displayed

At first I created this question, but it looks like I have two separate problems. To reduce the amount of code I have created a new project. In this project I create an app. I have added only one model:

from django.db import models
from django.utils.translation import gettext_lazy as _

class MyModel(models.Model):
    name = models.CharField(
        verbose_name=_('Field name'),
        max_length=20
    )

In the settings.py file I put the following:

MIDDLEWARE = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
]

...

LANGUAGE_CODE = 'uk'
TIME_ZONE = 'Europe/Kiev'
USE_I18N = True
USE_TZ = True

So after I made the migrations and created a superuser, I created the locale folder in the project root and tried to run this command:
django-admin makemessages -l uk

It successfully generated the .po file where I was able to add my translation:

#: .\myapp\models.py:7
msgid "Field name"
msgstr "Назва"

Then I tried the command:
django-admin compilemessages -l uk
And it generated a .mo file.

So everything looks fine, but when I go to the admin page and open my model, the field name is not translated. The interface is in Ukrainian, but in the model I see
image

Have I missed anything? Thank you for your help!

Have you customized your ModelAdmin class any? Are you using a custom form or template for this model?

Nothing. Created a project, created an app, added one model and registered it in the admin.py like this:

from django.contrib import admin
from .models import MyModel

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    pass

Caveat: I’ve never needed to use the translation functions - this is all new to me. Take everything I say here with a pound of salt.

I got this to work, but if I created the locale directory at the project level, I needed to create the LOCALE_PATHS setting and put the locale directory in it. It worked after that.

If I create the locale directory in the app, then Django appears to find it according to the docs, and no settings change was needed.

I tried to create the ‘locale’ folder inside my app, but then django-admin makemessages -l uk throws this:

CommandError: Unable to find a locale path to store translations for file catalog_init_.py. Make sure the ‘locale’ directory exists in an app or LOCALE_PATHS setting is set.

It appears that you either need to make your app directory your current directory when running makemessages, or ensure that there is a locale directory in every app and at the project level.

OK, now everything is clear :slight_smile: My translations finally work. Thank you so much!

1 Like