Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I18n translation of model form in Django

I have a form that I want to translate:

Models.py:

class Show(models.Model):
    discount_tickets = models.IntegerField("Discount Tickets")
    regular_tickets = models.IntegerField("Regular Tickets")
    afillate_price = models.IntegerField("Afillate Price")
    user_price = models.IntegerField("User Price")
    start_time = models.CharField("Event Time", max_length=20)
    sale_end_time = models.CharField("Sale End Time", max_length=20) 

    def __unicode__(self):
        return unicode(self.discount_tickets)

class ShowForm(ModelForm):
    pass

    class Meta:
        model = Show 

How can I translate the field names?

like image 369
misschoksondik Avatar asked Apr 08 '13 08:04

misschoksondik


People also ask

What is i18n in Django?

{% load i18n %} is needed for internationalization. The purpose of internationalization is to allow a single application to read in multiple languages. In order to do this: you need a few hooks called translation strings. To give your template access to these tags, put {% load i18n %} toward the top of your template..

What is Gettext_lazy in Django?

gettext_lazy() In definitions like forms or models you should use gettext_lazy because the code of this definitions is only executed once (mostly on django's startup); gettext_lazy translates the strings in a lazy fashion, which means, eg.

How does Django translation work?

Django then provides utilities to extract the translation strings into a message file. This file is a convenient way for translators to provide the equivalent of the translation strings in the target language. Once the translators have filled in the message file, it must be compiled.


1 Answers

from django.utils.translation import ugettext_lazy as _

class Show(models.Model):
    discount_tickets = models.IntegerField(_("Discount Tickets"))
    regular_tickets = models.IntegerField(_("Regular Tickets"))
    afillate_price = models.IntegerField(_("Afillate Price"))
    user_price = models.IntegerField(_("User Price"))
    start_time = models.CharField(_("Event Time"), max_length=20)
    sale_end_time = models.CharField(_("Sale End Time"), max_length=20) 
like image 67
catherine Avatar answered Sep 24 '22 23:09

catherine