Skip to content

Commit

Permalink
Run permission labels through translation on the permission managemen…
Browse files Browse the repository at this point in the history
…t template (#11923)

Since `{% trans some_variable %}` cannot be handled by makemessages, we also need to ensure that any string that arises from Wagtail's native permissions, such as "Can access Wagtail admin" or "Can view", exists somewhere in the python code as a `_("...")` value.

Fixes #5341
  • Loading branch information
gasman committed May 3, 2024
1 parent 538365f commit 8aaa579
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Expand Up @@ -9,6 +9,7 @@ Changelog
* Refactor redirects edit view to use the generic `EditView` and breadcrumbs (Rohit Sharma)
* Fix: Make `WAGTAILIMAGES_CHOOSER_PAGE_SIZE` setting functional again (Rohit Sharma)
* Fix: Enable `richtext` template tag to convert lazy translation values (Benjamin Bach)
* Fix: Ensure permission labels on group permissions page are translated where available (Matt Westcott)
* Docs: Remove duplicate section on frontend caching proxies from performance page (Jake Howard)
* Maintenance: Use `DjangoJSONEncoder` instead of custom `LazyStringEncoder` to serialize Draftail config (Sage Abdullah)

Expand Down
1 change: 1 addition & 0 deletions docs/releases/6.2.md
Expand Up @@ -22,6 +22,7 @@ depth: 1

* Make `WAGTAILIMAGES_CHOOSER_PAGE_SIZE` setting functional again (Rohit Sharma)
* Enable `richtext` template tag to convert lazy translation values (Benjamin Bach)
* Ensure permission labels on group permissions page are translated where available (Matt Westcott)


### Documentation
Expand Down
3 changes: 2 additions & 1 deletion wagtail/admin/models.py
@@ -1,5 +1,6 @@
from django.contrib.contenttypes.models import ContentType
from django.db.models import Count, Model
from django.utils.translation import gettext_lazy as _
from modelcluster.fields import ParentalKey
from taggit.models import Tag

Expand All @@ -18,7 +19,7 @@ class Admin(Model):
class Meta:
default_permissions = [] # don't create the default add / change / delete / view perms
permissions = [
("access_admin", "Can access Wagtail admin"),
("access_admin", _("Can access Wagtail admin")),
]


Expand Down
3 changes: 2 additions & 1 deletion wagtail/contrib/simple_translation/models.py
@@ -1,5 +1,6 @@
from django.conf import settings
from django.db.models import Model
from django.utils.translation import gettext_lazy as _

from wagtail import hooks
from wagtail.models import Locale
Expand All @@ -17,7 +18,7 @@ class SimpleTranslation(Model):
class Meta:
default_permissions = []
permissions = [
("submit_translation", "Can submit translations"),
("submit_translation", _("Can submit translations")),
]


Expand Down
Expand Up @@ -96,7 +96,8 @@
<fieldset class="w-p-0">
<legend class="w-sr-only">{% trans "Custom permissions" %}</legend>
{% for custom_perm in content_perms_dict.custom %}
{% include "wagtailadmin/shared/forms/single_checkbox.html" with name="permissions" value=custom_perm.perm.id checked=custom_perm.selected text=custom_perm.name attrs=custom_perm.attrs %}
{% trans custom_perm.name as custom_perm_label %}
{% include "wagtailadmin/shared/forms/single_checkbox.html" with name="permissions" value=custom_perm.perm.id checked=custom_perm.selected text=custom_perm_label attrs=custom_perm.attrs %}
{% endfor %}
</fieldset>
{% endif %}
Expand Down Expand Up @@ -170,7 +171,7 @@
<tbody>
{% for perm_tuple in other_perms %}
<tr>
<td class="title"><label for="{{ perm_tuple.1.id_for_label }}" class="w-label-3">{{ perm_tuple.0.name }}</label></td>
<td class="title"><label for="{{ perm_tuple.1.id_for_label }}" class="w-label-3">{% trans perm_tuple.0.name %}</label></td>
<td>
{{ perm_tuple.1.tag }}
</td>
Expand Down
9 changes: 9 additions & 0 deletions wagtail/users/templatetags/wagtailusers_tags.py
Expand Up @@ -6,6 +6,7 @@
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.utils.text import camel_case_to_spaces
from django.utils.translation import gettext_noop

from wagtail import hooks
from wagtail.admin.models import Admin
Expand Down Expand Up @@ -55,6 +56,14 @@ def normalize_permission_label(permission: Permission):
return label


# normalize_permission_label will return "Can view" for Django's standard "Can view X" permission.
# formatted_permissions.html passes these labels through {% trans %} - since this is a variable
# within the template it will not be picked up by makemessages, so we define a translation here
# instead.

VIEW_PERMISSION_LABEL = gettext_noop("Can view")


@register.inclusion_tag("wagtailusers/groups/includes/formatted_permissions.html")
def format_permissions(permission_bound_field):
"""
Expand Down

0 comments on commit 8aaa579

Please sign in to comment.