aikchun
aikchun

Reputation: 115

How do I translate django admin form labels?

I have a django project using django modeltranslation to do translation for my models' names. In this case, I have name_en and name_zh_hans.

enter image description here

As shown, I am able to translate the word 'name' into chinese but not the auto generated '[en]' and '[zh-hans]' part of the label. I've look through the django's and modeltranslation's documentation but still couldn't figure it out. Have anyone met with the same problem and manage to resolve it?

Upvotes: 1

Views: 1611

Answers (2)

ChrisRob
ChrisRob

Reputation: 1552

build_localized_verbose_name in modeltranslation/utils.py creates these labels. Right now, there seems to be no easy way to customize this.

My workaround was to use a custom form which changes every label:

class UserForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field_name in self.fields.keys():
            label = self.fields[field_name].label
            if label:
                label = label.replace(' [de]', ' - Deutsch').replace(' [en]', ' - English')
                self.fields[field_name].label = label


class UserAdmin(TranslationAdmin):
    form = UserForm

Upvotes: 0

matyas
matyas

Reputation: 2796

To display a diffrent label of a model field in the admin section one can use the verbose_name parameter when declaring models:

from django.utils.translation import ugettext_lazy as _

class Country(models.Model):
    name = models.CharField(verbose_name=_("This string will appear in the admin"), max_length=100)

the [en] and [zh-hans] parts of the label are only showing information about the language of the field that will be edited and should have no effect on the tranlsation at all.

Upvotes: 1

Related Questions