django is not using my custom translation

Viewed 31

I have a django project with django rest framwork. I want a multi language project and I am trying to have my custom translation. I use drf document https://www.django-rest-framework.org/topics/internationalization/#adding-new-translations and also django document and other sources but know django is still using drf default translation. my overriding is not applied and new strings are no translated. eg I override this line in .po file

msgid "Username"
msgstr ""

to

msgid "Username"
msgstr "یوزنیم"

but it is translated to "نام کاربری" and I add this line

msgid "there is a user with this national id"
msgstr "سامتینگ"

and use that message as a serializer unique validator error but it is not translated

national_id = serializers.CharField(validators=[validate_national_id, UniqueValidator(queryset=User.objects.all(), message='there is a user with this national id') ], allow_blank=True)
1 Answers

You should mark the string as a translation string:

from django.utils.translation import gettext as _

national_id = serializers.CharField(validators=[validate_national_id, UniqueValidator(queryset=User.objects.all(), message=_('there is a user with this national id')) ], allow_blank=True)

read the usage of translation https://docs.djangoproject.com/en/4.0/topics/i18n/translation/

Related