1

I have two questions. How can I translate in forms: a) labels b) Form ValidationErrors ?

forms.py

from django import forms
from django.db.models import Max, Min
from .models import Application, FlowRate, WaterFilterSubType, Fineness, Dirt
from .models import DirtProperties, Flange

class InputDataWaterForm(forms.Form):
    '''Application'''
    choices = list(Application.objects.values_list('id','application'))
    application = forms.ChoiceField(choices = choices, initial="1", label="Specify application:")
    ....

    def clean(self):
        cleaned_data = super(InputDataWaterForm, self).clean()
        application = cleaned_data.get('application')
    ...

        '''OTHER CONDITIONS if not flowrate ...'''
        if not (flowrate or pressure or dirt or dirtproperties or
            application or fineness or temperature or
            flange or atex or aufstellung or ventil):
            raise forms.ValidationError('PLEASE ENTER THE REQUIRED INFO')

How can I translate content of a table in a database? All records eg. product names in a table must be translated.

models.py

from django.db import models

'''FILTER PROPERTIES LIKE COLOR'''
class FP_sealing(models.Model):
    value = models.CharField('Material Sealing', max_length=10)
    descr = models.CharField('Description', max_length=200, default="")
    def __str__(self):
        return("Seal: " + self.value)

Thank you

cegthgtlhj
  • 191
  • 1
  • 8

1 Answers1

1

Translations are normally done with Django's translation framework. For eager translations, one uses gettext, for lazy translations (translations that should be calculated when rendered), one uses gettext_lazy. You can for example translate your application with:

from django import forms
from django.db.models import Max, Min
from .models import Application, FlowRate, WaterFilterSubType, Fineness, Dirt
from .models import DirtProperties, Flange
from django.utils.translation import gettext_lazy as _

class InputDataWaterForm(forms.Form):
    '''Application'''
    choices = list(Application.objects.values_list('id','application'))
    application = forms.ChoiceField(choices = choices, initial="1", label=_("Specify application:"))
    # …

    def clean(self):
        cleaned_data = super(InputDataWaterForm, self).clean()
        application = cleaned_data.get('application')
        # …

        '''OTHER CONDITIONS if not flowrate …'''
        if not (flowrate or pressure or dirt or dirtproperties or
            application or fineness or temperature or
            flange or atex or aufstellung or ventil):
            raise forms.ValidationError(_('PLEASE ENTER THE REQUIRED INFO'))

For choice values in models, as is specified in the documentation:

from django.utils.translation import gettext_lazy as _

class Student(models.Model):

    class YearInSchool(models.TextChoices):
        FRESHMAN = 'FR', _('Freshman')
        SOPHOMORE = 'SO', _('Sophomore')
        JUNIOR = 'JR', _('Junior')
        SENIOR = 'SR', _('Senior')
        GRADUATE = 'GR', _('Graduate')

    year_in_school = models.CharField(
        max_length=2,
        choices=YearInSchool.choices,
        default=YearInSchool.FRESHMAN,
    )

    def is_upperclass(self):
        return self.year_in_school in {
            self.YearInSchool.JUNIOR,
            self.YearInSchool.SENIOR,
        }

Then you can run the makemessages command [Django-doc] to make translation files:

django-admin makemessages --locale=de_DE

Django will then make *.po files where you can define the translations for the strings you defined.

Then you can use the compilemessages command [Django-doc] to compile these translation files to *.mo files:

django-admin compilemessages --locale=de_DE
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • and my second question please. Which tools are available to translate content (a text field) of a table within a database? Eg. translation / localization of an online shop. The names of products and their descriptions must be tranlated. Thank you. – cegthgtlhj Apr 03 '20 at 15:52
  • 1
    @cegthgtlhj: you pass it through the `_(..)` as well. But then you will need to add the translations to the `*.po` files yourself, since Django can know exactly what values should be translated. That being said, if you have choices, then you normally use `_(..)` over the *value* part of the 2-tuples. Such that the translation is done by Django. – Willem Van Onsem Apr 03 '20 at 15:54
  • I means such a case, please: `view.py qset = Products.objects.all() Template.html {% for i in qset %} {{ i.product_name }} ` How can I translate such table values? Thank you – cegthgtlhj Apr 03 '20 at 16:17
  • 1
    @cegthgtlhj: well since the values are arbitrary, you will need to make some extra models that store the product name in different languages. See for example https://goodcode.io/articles/django-multilanguage/ – Willem Van Onsem Apr 03 '20 at 16:20