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

'Can access Wagtail admin' is not localized #5341

Closed
children1987 opened this issue May 30, 2019 · 10 comments
Closed

'Can access Wagtail admin' is not localized #5341

children1987 opened this issue May 30, 2019 · 10 comments
Labels
component:i18n i18n for content created in Wagtail, not the admin UI itself type:Bug

Comments

@children1987
Copy link

I found there is a problem about localization, as this img:
image

I think it should be change to:

from django.utils.translation import gettext_lazy as _
# Create admin permission
admin_permission, created = Permission.objects.get_or_create(
    content_type=wagtailadmin_content_type,
    codename='access_admin',
    name=_('Can access Wagtail admin')
)
@loicteixeira loicteixeira added component:i18n i18n for content created in Wagtail, not the admin UI itself type:Bug labels Sep 29, 2019
@loicteixeira loicteixeira changed the title A bug about localization of 'Can access Wagtail admin' 'Can access Wagtail admin' is not localized Sep 29, 2019
@kaedroho
Copy link
Contributor

kaedroho commented Jul 8, 2020

This is tricky because this string is stored in the database. Running gettext on it in a migration will have no effect as gettext won't translate things in a migration (it doesn't do anything unless a language has been activated by something like LocaleMiddleware).

A possible but hacky looking solution could be to pass the database value into gettext when it's displayed, for example:

_(permission.name)

or:

{% trans permission.name %}

As long as the literal value is defined somewhere (eg the migration) so that gettext can extract it, the string should have a translation that it could use. The only issue here would be if someone manually changed this string in the database.

@thibaudcolas
Copy link
Member

@kaedroho when you have the chance, could you add more info on where to do what you suggested? Is it in formatted_permissions.html? This seems like a good "good first issue" if we can tell people exactly where to change this.

@gasman
Copy link
Collaborator

gasman commented Apr 11, 2022

See also #7749.

I can see a possible gotcha with @kaedroho's suggestion - if the translated string is part of the migration, and a non-English locale is active when running migrations (e.g. LANGUAGE_CODE is set), it'll end up inserting the non-English text in the database, which then won't be translated by {% trans permission.name %}. Given the untried nature of this, I don't think it's a good fit for "good first issue".

@thibaudcolas
Copy link
Member

As far as I understand, this is exactly what @kaedroho flags, and his suggestion is to call gettext when the database value is displayed while the migration would be kept as-is.

@gasman
Copy link
Collaborator

gasman commented Apr 11, 2022

It's more subtle than that - there needs to be an occurrence of _('Can access Wagtail admin') somewhere in the codebase so that it gets picked up by makemessages, but that occurrence can NOT be anywhere that would be encountered when creating the permission record in the database (because that would result in it inserting the translated record into the database rather than the English one).

@thibaudcolas
Copy link
Member

I understood Karl as suggesting we’d have a dummy _('Can access Wagtail admin') somewhere for the purpose of extraction of the message. So:

  1. No changes to the value stored in the database
  2. Somewhere, we have a dummy gettext call with the same literal value as what’s stored in the database, for the purpose of collecting by makemessages.
  3. We also call gettext on the database value, which will leverage the fact we have a dummy translation string elsewhere

@SebCorbin
Copy link
Contributor

SebCorbin commented May 2, 2024

When declaring custom model permissions as documented in Django,

class Mymodel(models.Model):
    class Meta:
        permissions = [("can_deliver_pizzas", _("Can deliver pizzas"))]

the migration generated is:

migrations.AlterModelOptions(
    name="mymodel",
    options={
        "permissions": [("can_deliver_pizzas", "Can deliver pizzas")],
        "verbose_name": "Mymodel",
    },
),

so this seems quite safe to only try to translate in the template:

{% for custom_perm in content_perms_dict.custom %}
    {% trans custom_perm.name as perm_name %}
    {% include "wagtailadmin/shared/forms/single_checkbox.html" with name="permissions" value=custom_perm.perm.id checked=custom_perm.selected text=perm_name %}
{% endfor %}

@gasman
Copy link
Collaborator

gasman commented May 2, 2024

@SebCorbin The question is, are we 100% confident that there will never be a situation - in anyone's Wagtail project - where a language other than English is active while makemigrations is running, causing the value

("access_admin", _("Can access Wagtail admin"))

to be resolved to something like

("access_admin", "Kann Wagtail-Admin zugreifen")

while it's scanning for changes to the model state, making it think that there's been a change from what the existing migrations say, and generating a bogus migration in the wagtailadmin app?

I've just been trying this out, and haven't been able to break it in that way - but I couldn't be confident that it could never happen, without some very careful reading of the Django source code.

Having said that, I see no downside to placing _("Can access Wagtail admin") somewhere totally benign in the code as per @thibaudcolas's suggestion - so I'll go ahead and create a PR for that.

@gasman
Copy link
Collaborator

gasman commented May 2, 2024

Actually, I now see that we've been using that pattern on the Page model's permissions since at least 5.1, so a) if it wasn't safe we'd probably have seen it break by now, and b) we have nothing to gain by avoiding it here :-)

gasman added a commit to gasman/wagtail that referenced this issue May 2, 2024
…t template

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 wagtail#5341
@SebCorbin
Copy link
Contributor

SebCorbin commented May 3, 2024

@SebCorbin The question is, are we 100% confident that there will never be a situation - in anyone's Wagtail project - where a language other than English is active while makemigrations is running

I would say yes, because the makemigrations command is decorated to not take translations into account but I'm only 99% sure :)

@gasman gasman closed this as completed in 8aaa579 May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:i18n i18n for content created in Wagtail, not the admin UI itself type:Bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants