Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django form field label translations

I have a baseform with over 20 fields. Then I have about 15 other forms inheriting from that form, passing in a parameter called fields which the baseform uses to delete all other fields. Best explain via example:

class BaseForm(forms.Form):
    reportid = forms.HiddenInput()
    fromdate = forms.DateField(label=_("From"), widget=widgets.AdminDateWidget())
    todate = forms.DateField(label=_("To"), widget=widgets.AdminDateWidget())
    sort_by = forms.ChoiceField(label=_("Sort by"), choices=[])
    .......

    def __init__(self, *args, **kwargs):
        fields = kwargs.pop('fields')
        #Pseudo:
        ***del self.fields[field] for field not in fields***

class SubForm(forms.Form):
    def __init__(self, *args, **kwargs):
        fields = ['reportid', 'todate']
        super(SubForm, self).__init__(fields=fields, *args, **kwargs)

The resulting form would then look like this:

class SubForm(forms.Form):
    reportid = forms.HiddenInput()
    todate = forms.DateField(label=_("To"), widget=widgets.AdminDateWidget())

My problem is that when the BaseForm is initialized for the first time, the labels are bound to the fields with the active language, and when another user logs in with another language setting (or the current user changes languages) the field labels don't update.

I've come to a solution using a dict like this:

labels = {
    'todate': lambda: _("To"),
    'fromdate': lambda: _("From"),
    .....
}

and then when initializing the baseform looping through all fields and setting

self.fields[field].widget.label = labels[field]()

Do I have any nicer (read: more pythonic) way of achieving this?

like image 214
Sindri Guðmundsson Avatar asked Sep 29 '11 10:09

Sindri Guðmundsson


1 Answers

Django provides _lazy variants of the translation functions (for example ugettext_lazy) so you can ark strings for translations at the access time (as opposed to when the translation function is called).

It's documented in details at https://docs.djangoproject.com/en/1.7/topics/i18n/translation/#lazy-translation

like image 148
Krzysztof Klimonda Avatar answered Oct 19 '22 19:10

Krzysztof Klimonda