forms.ModelForm django translation

Dear community,
I am trying to use the django translation built-in functions to translate the labels of the form.

I report here the code that i am using,

models.py

from django.contrib.gis.db import models
from django.contrib.auth.models import User

from farmer.models import Farmer

# Create your models here.

class Field(models.Model):
    Farmer = models.ForeignKey(Farmer,
                               blank=True, 
                               null=True, 
                               on_delete=models.CASCADE)
    
    FieldName = models.CharField(max_length=500)
    
    User = models.ForeignKey(User, 
                             blank=True, 
                             null=True, on_delete=models.CASCADE)

    def __str__(self):
        return f'{self.FieldName}'

forms.py

from django import forms
from leaflet.forms.widgets import LeafletWidget

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Field

from .models import Field
from farmer.models import Farmer

from django.utils.translation import gettext as _

class FieldForm(forms.ModelForm):
    class Meta:
        model = Field
        fields = ('Farmer', 'FieldName')
        labels = {
            "Farmer": _("Name of the farmer company"),
            "FieldName": _("Name of the field")
        }
        widgets = {
        }
    
    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        super(FieldForm, self).__init__(*args, **kwargs)
        
        if user is not None:
            self.fields['Farmer'].queryset = Farmer.objects.filter(User_id=user)

views.py

class NewFieldFarmer(LoginRequiredMixin, CreateView):
    model = Field
    form_class = FieldForm
    template_name = 'field/field_new.html'
    success_url="/registeredfield/"

urls.py (app directory)

from django.urls import path

from .views import NewFieldFarmer

urlpatterns = [
    path("newfield/", NewFieldFarmer.as_view(), name="newfield")
]

urls.py (project directory

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path("", include("field.urls")),
]

field_new.html

{% extends "base.html" %}
{% load crispy_forms_tags %}
{% load i18n %}

{% block content %}
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    {% leaflet_js plugins="forms, geocoder, measurement" %}
    {% leaflet_css plugins="forms, geocoder, measurement" %}
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <title>{% trans "New Field" %}</title>
  </head>
  <body>
    <br>
    <h3 style="text-align: center;">{% trans "New field registration" %}</h3>
    <hr>
    <div class="container">
        <div class="row align-items-start">
            <div class="col-8">
                <form method="POST">
                    {% csrf_token %}
                    {{ form|crispy }}
                    <input class= "btn btn-success" type="submit" value="Register the new field"/>
                </form>           
                <br>
            </div>
        </div>
    </div>
</div>
</body>
</html>
{% endblock %}

what am i doing wrong?

I assume that you’re not seeing the translated strings on the template.
In that case i should ask, have you written the translation to the desired language using the makemessages command, and after that runned the compilemessages command?
Also make sure you have followed the documentation on translations. There’s a important note there:

Make sure you’ve activated translation for your project (the fastest way is to check if MIDDLEWARE includes django.middleware.locale.LocaleMiddleware). If you haven’t yet, see How Django discovers language preference.

Thanks to reply @leandrodesouzadev ,
Yes i have added the middleware to the settings.py and also i have used this sequence.

  1. Insert the trans tag

  2. use

django-admin makemessages -l it
  1. translate the meaning

  2. use

django-admin compilemessages -l it

I checked if the same word is translated in another section of the site (template) and it is translated correctly.

But when I try to insert it into modelforms, it doesn’t translate for me.

Ok translation
Screenshot 2023-09-20 144942

No translation in the form

I have solved the issue by replacing in the forms.py this

from django.utils.translation import gettext as _

with this

from django.utils.translation import gettext_lazy as _