django-rest-framework
django-rest-framework copied to clipboard
Serializer translation not translate with named-string
I use the Django translation apply with serializer as following
from django.contrib.auth.hashers import make_password
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
from .models import SampleModel
class RegisterSerializer(serializers.Serializer):
...
first_name = serializers.CharField(max_length=50, error_messages={
'required': _("First name is required"),
'max_length': _("First name must not be greater than %(max_length)s characters") % { "max_length": 50 },
})
...
def create(self, validated_data):
SampleModel.objects.create(
...
first_name=validated_data["first_name"],
...
)
The locale files root/locale/en/LC_MESSAGES/django.mo
msgid "First name is required"
msgstr ""
msgid "First name must not be greater than %(max_length)s characters"
msgstr ""
And another language file, I just named it fr
The locale files root/locale/fr/LC_MESSAGES/django.mo
msgid "Le prénom est requis"
msgstr ""
msgid "Le prénom ne doit pas dépasser %(max_length)s caractères"
msgstr ""
After translated I use command to compile
python3 manage.py compilemessages
The issue is when I use named-string placeholder it not translate, but if I try to remove named-string placeholder it work normally.