如何在 Django 中翻译表单?

发布于 2024-12-22 06:27:47 字数 383 浏览 1 评论 0原文

我想翻译 django 形式。如何翻译字段标签?

我尝试了 field.label=ugettext_lazy(field.label),但是标签没有填充在 django.po 文件中

我可能错误地理解了 ugettext_lazy 的概念,我想

简单来说我想要要放入 django.po 文件中的字段标签。

使用 ugettext{% trans %} 标签完成的其他翻译运行良好

我已经能够通过设置 verbose_name 但当我在表单字段中尝试这样做时,我得到一个 TypeError

I want to translate a django form. How do I translate the labels of the fields?

I tried field.label=ugettext_lazy(field.label), but the labels are not getting populated in django.po file

I may have gotten the concept of ugettext_lazy wrong, I think

In simple terms I want the field labels to be put into django.po file.

The other translations done using ugettext and {% trans %} tag are working well

I have been able to translate the fields based on a model by setting verbose_name but when I try that for a form field I get a TypeError

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

财迷小姐 2024-12-29 06:27:48

我会精确地说:

from django.utils.translation import ugettext_lazy as _
...
first_name = forms.CharField(label=_(u'First name'))

它可能会引发错误,因为表单无法很好地管理像这样的代理对象
_(u'First name'),渲染结果是一个void形式。
我在 python2.x 和 django 1.3/1.4 上对此进行了测试,

原因是由于最初由不同操作系统和库创建的已编译 .po 消息(它可能取决于 python、django、os.libraries 版本)。
当您遇到此错误时,您必须重新创建本地化消息。

I will precise that :

from django.utils.translation import ugettext_lazy as _
...
first_name = forms.CharField(label=_(u'First name'))

It's likely to raise an error because forms cannot manage well a proxy object like
_(u'First name'), and the result in rendering is a void form.
I tested this on python2.x and django 1.3/1.4

The reason is due to the compiled .po messages initially created by different o.s. and libraries (it can depend by python,django,os. libraries versions).
When you have this error you must re-create the localized messages.

远昼 2024-12-29 06:27:48
class ExampleForm(forms.Form):
    f1 = forms.CharField(label= ugettext_lazy('field label'))
class ExampleForm(forms.Form):
    f1 = forms.CharField(label= ugettext_lazy('field label'))
温柔少女心 2024-12-29 06:27:48
from django.utils.translation import ugettext_lazy as _
first_name = forms.CharField(label=_(u'First name'))
from django.utils.translation import ugettext_lazy as _
first_name = forms.CharField(label=_(u'First name'))
苍白女子 2024-12-29 06:27:48

作为替代方法,您可以将脚本添加到 template.html

<script>
        labels = document.getElementsByTagName('label');
        for (var j = 0; j < labels.length; j++) {   
             if (labels[j].textContent == "Username:") labels[j].textContent =    "Логін   ";
             if (labels[j].textContent == "First name:") labels[j].textContent =  "Ім'я    ";
             if (labels[j].textContent == "Last name:") labels[j].textContent =   "Прізвище";
             if (labels[j].textContent == "Email address:") labels[j].textContent =   "Email адреса ";
             if (labels[j].textContent == "Password:") labels[j].textContent =        "Пароль            ";
             if (labels[j].textContent == "Confirm password:") labels[j].textContent =        "Підтвердити пароль";
             };
</script>

As an alternative way, you can add script to your template.html

<script>
        labels = document.getElementsByTagName('label');
        for (var j = 0; j < labels.length; j++) {   
             if (labels[j].textContent == "Username:") labels[j].textContent =    "Логін   ";
             if (labels[j].textContent == "First name:") labels[j].textContent =  "Ім'я    ";
             if (labels[j].textContent == "Last name:") labels[j].textContent =   "Прізвище";
             if (labels[j].textContent == "Email address:") labels[j].textContent =   "Email адреса ";
             if (labels[j].textContent == "Password:") labels[j].textContent =        "Пароль            ";
             if (labels[j].textContent == "Confirm password:") labels[j].textContent =        "Підтвердити пароль";
             };
</script>
海拔太高太耀眼 2024-12-29 06:27:48

您应该使用 gettext_lazy()标签forms.CharField() 中,如下所示:

from django.utils.translation import gettext_lazy as _

first_name = forms.CharField(label=_('First name'))

You should use gettext_lazy() and label in forms.CharField() as shown below:

from django.utils.translation import gettext_lazy as _

first_name = forms.CharField(label=_('First name'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文