翻译 Django 组名

发布于 2025-01-18 12:28:20 字数 242 浏览 1 评论 0原文

我正在创建一个 Django 应用程序,其中用户将有不同的可能组来管理他们的权限。

我希望这个应用程序将来可以翻译,但默认情况下组名称不可翻译。我怎样才能使这成为可能?我考虑了几种可能的解决方案,但无法决定哪一种是最好的。

  • 替换显示组的模板并翻译组名称。
  • 替换组模板并更改 __str__ 方法。

你能想到另一个解决方案吗?

您认为哪一款最好?

I am creating a Django application where users will have different possible groups to manage their permissions.

I would like this application to be translatable in the future, but the group names are not translatable by default. How can I make this possible? I have thought about a few possible solutions, but I can't decide which one is best.

  • Replace the template where the groups are displayed and translate the group names.
  • Replace the group templates and change the __str__ method.

Can you think of another solution?

Which one do you think is the best?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再浓的妆也掩不了殇 2025-01-25 12:28:20

我最终决定像这样重写 Group 模型的 __str__ 方法(​​在 models.py 中)

from django.contrib.auth.models import Group
from django.utils.translation import gettext as _

Group.__str__ = lambda self : _(self.name)

I finally decided to override the __str__ method of the Group model like that (in models.py)

from django.contrib.auth.models import Group
from django.utils.translation import gettext as _

Group.__str__ = lambda self : _(self.name)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文