django icon indicating copy to clipboard operation
django copied to clipboard

translation fix for content_type app_label

Open mh-firouzjah opened this issue 2 years ago • 1 comments

mh-firouzjah avatar Apr 09 '22 15:04 mh-firouzjah

Hello there. I actually made this change to have full translation power on the admin panel, especially for User permissions where there is a list of permission names, part of which was in English and part in another language if the language was not set to English. It's part one of required changes but there is still need for another change the Permission object will be auto generated and there are 4 common names for permissions:

  • Can add Content_type
  • Can change Content_type
  • Can delete Content_type
  • Can view Content_type

and the str method of the Permission class will result in sth like:

  • نشست‌ها | نشست | Can view session

for that name to be fully translated, the process of auto generation the name must consider to mark that part for translation.

  • also I've a question: why the name of content_type has been repeated in the name of permission object? Can view session | session it could be Can view | session.

  • by the way I prepared a monkey patch to fix the problem for my self:

# admin.py
from django.contrib.auth.models import Permission
from django.utils.translation import gettext_lazy as _


def perm_str(self):
    if "add_" in self.codename:
        return _("Can add | {content_type}").format(content_type=self.content_type)
    elif "change_" in self.codename:
        return _("Can change | {content_type}").format(content_type=self.content_type)
    elif "delete_" in self.codename:
        return _("Can delete | {content_type}").format(content_type=self.content_type)
    elif "view_" in self.codename:
        return _("Can view | {content_type}").format(content_type=self.content_type)


Permission.__str__ = perm_str # replace the __str__ method so to use the translation-provided one!
admin.site.register(Permission) # this line is not necessary
  • before applying changes: Screenshot_20220409_204838

  • after change: Screenshot_20220409_205122

mh-firouzjah avatar Apr 09 '22 16:04 mh-firouzjah

Closing in favor of #16053.

felixxm avatar Sep 14 '22 07:09 felixxm