繁体   English   中英

Django 翻译国际化和本地化:forms,模型,表格

[英]Django Translation Internationalization and localization: forms, models, tables

我有两个问题。 如何在 forms 中翻译:a) 标签 b) 表单验证错误?

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')

如何翻译数据库中表的内容? 所有记录,例如。 必须翻译表中的产品名称。

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)

谢谢

翻译通常使用Django 的翻译框架完成。 对于急切的翻译,使用gettext ,对于惰性翻译(应在渲染时计算的翻译),使用gettext_lazy 例如,您可以使用以下方式翻译您的应用程序:

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'))

对于模型中的选择值,如文档中所述:

 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, }

然后你可以运行makemessages命令 [Django-doc]来制作翻译文件:

django-admin makemessages --locale=

Django 然后将生成*.po文件,您可以在其中定义您定义的字符串的翻译。

然后您可以使用compilemessages命令 [Django-doc]将这些翻译文件编译为*.mo文件:

django-admin compilemessages --locale=

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM