如何在 DRF 中仅翻译自定义错误消息?

How to translate only custom error messages in DRF?

在 DRF 中,所有默认的错误消息都已经运行安排好了。但我需要 t运行 列出我自己的错误消息。 我做了什么:

  1. 将我所有的错误信息放入 _(),其中 _gettext
  2. 在设置中将 LOCALE_PATHS 设置为 locale 文件夹
  3. 运行 python manage.py makemessages -l ru

创建了 .po 文件,第一个麻烦就来了。在 .po 文件中是我的消息,除此之外还有很多我不想 t运行slate 的默认 Django 消息。 (我不想覆盖 t运行slation,我想扩展它)

我运行在 .po 文件中列出了我的消息,然后我 运行 python manage.py compilemessages 创建了 .mo 文件。还有第二个麻烦。

我所有的消息现在都已 t运行 预定,但默认 DRF 消息(它们不在 .po 文件中,有 Django 消息,而不是 DRF)尚未 t运行 预定,它们只有英文(例如 Authentication credentials were not providedThis field is required 等)

需要从 venv 中的包中排除消息。 例如

--忽略

python manage.py makemessages -l ru --ignore=env
python manage.py compilemessages -l ru --ignore=env