Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC – Fixed #1688 -- Output translatable labels when displaying permissions #1

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion django/contrib/auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,18 @@ class Meta:
ordering = ["content_type__app_label", "content_type__model", "codename"]

def __str__(self):
return "%s | %s" % (self.content_type, self.name)
names = { "content_type": self.content_type }

if "Can add" in self.name:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would startswith be a more accurate check?

Suggested change
if "Can add" in self.name:
if self.name.startswith("Can add "):

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely. And the fallback after those conditionals should probably be return "%s | %s" % (self.content_type, _(self.name)), so custom permissions are translate-able (would additionally require people to hard-code a copy of their permission names somewhere in their code for makemessages to pick up).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah cool 👍

return _("%(content_type)s | Can add") % names
if "Can change" in self.name:
return _("%(content_type)s | Can change") % names
if "Can delete" in self.name:
return _("%(content_type)s | Can delete") % names
if "Can view" in self.name:
return _("%(content_type)s | Can view") % names

return "%s | %s | %s" % (self.content_type, self.name)

def natural_key(self):
return (self.codename,) + self.content_type.natural_key()
Expand Down