【问题标题】:How to translate only custom error messages in DRF?如何仅翻译 DRF 中的自定义错误消息?
【发布时间】:2020-10-18 14:43:52
【问题描述】:

在 DRF 中,所有默认错误消息都已翻译。但我需要翻译我自己的错误信息。 我做了什么:

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

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

我在 .po 文件中翻译了我的消息,然后我运行 python manage.py compilemessages 创建了 .mo 文件。这是第二个麻烦。

我的所有消息现在都已翻译,但默认的 DRF 消息(它们不在 .po 文件中,有 Django 消息,不是 DRF)尚未翻译,它们只有英文(例如 Authentication credentials were not provided、@987654329 @等)

【问题讨论】:

    标签: python django django-rest-framework internationalization translation


    【解决方案1】:

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

    --忽略

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

    【讨论】:

      猜你喜欢
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      相关资源
      最近更新 更多