Django -翻译权限模型的名称

fkaflof6  于 2023-06-25  发布在  Go
关注(0)|答案(2)|浏览(141)

我正在尝试翻译权限的名称,但它不适用于此代码。我没有得到任何错误。权限仍然是英语,我希望他们在设置中设置的语言。知道是什么吗

# forms.py
from django.utils.translation import gettext as _
class PermissionModelMultipleChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj):
    return '%s' % _(obj.name)

# settings.py
LANGUAGE_CODE = 'pt-br'
USE_I18N = True
USE_L10N = True
ncgqoxb0

ncgqoxb01#

我能够手动翻译权限的名称。有些像“Can”gettext_lazy这样的词无法翻译,所以我使用了“replace”方法来离开我想要的语言。其他单词如“group”或“user”gettext_lazy可以翻译。

# forms.py
from django import forms
from django.utils.translation import gettext_lazy as _

class PermissionModelMultipleChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj):
    permissions_translated = [_(w).replace('Can', 'Pode').replace('add', 'adicionar').replace('change', 'alterar').replace('delete', 'excluir').replace('view', 'visualizar') for w in (obj.name).split()]
    return ' '.join(permissions_translated)
i7uq4tfw

i7uq4tfw2#

这段代码可以帮助你。

class RoleCreateForm(forms.ModelForm):
    permissions = forms.ModelMultipleChoiceField(
        queryset=Permission.objects.none(),
        widget=forms.SelectMultiple(attrs={
            'class':'selectpicker widget border border-2 border-warning rounded bg-transparent'
        })
    )

    class Meta:
        model = Group
        fields = "__all__"

        widgets={
            'name':forms.TextInput(attrs={
                'class':'form-control widget border-warning rounded'
            })
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Filter permissions for tracking, traccar, and accounts apps
        tracking_permissions = Permission.objects.filter(content_type__app_label='tracking').exclude(content_type__model='geoeventsetting')
        traccar_permissions = Permission.objects.filter(content_type__app_label='traccar', content_type__model__in=['tcdevices', 'tcreports'])
        accounts_permissions = Permission.objects.filter(content_type__app_label='accounts',content_type__model__in=['useraccount'])
        auth_permissions = Permission.objects.filter(content_type__app_label='auth',content_type__model__in=['group'])

        # Concatenate the filtered permissions and set them as the choices for the permissions field
        self.fields['permissions'].queryset = tracking_permissions | traccar_permissions | accounts_permissions |auth_permissions

        # Translate the permission names
        self.fields['permissions'].label_from_instance = self.label_permission_instance

    def label_permission_instance(self, obj):
        permission_name = obj.name

        permissions_translated =[w.replace('Can', 'Peut').replace('add', 'ajouter').replace('change', 'modifier').replace('delete', 'supprimer').replace('view', 'voir').replace('user', 'utilisateur').replace('tc', '').replace('devices', 'boitiers').replace('reports', 'rapports').replace('group', 'profil')   for w in permission_name.split()]
        return ' '.join(permissions_translated)

相关问题