Django:ChoiceField 表单中的本地化问题

发布于 2024-11-06 03:58:41 字数 1122 浏览 1 评论 0原文

在表单中使用 ChoicesField 时,我在使用 Django 的翻译功能时遇到了麻烦。

我使用这样的表单:

from django.utils.translation import ugettext as _

class MyForm(forms.Form):
COUNTRIES = (
    ('france', _('France')),
    ('italy', _('Italy')),
    ('sweden', _('Sweden')),
)

country = forms.ChoiceField(choices=COUNTRIES)

通过模板中的视图呈现,例如这样 ... {% trans "国家" %}: {{ form.country }} ...

我的问题与口音有关,例如在法国翻译时的这个例子:

<label>Contrée </label>
<select name="country" id="id_country">
<option value="france">France</option>
<option value="italy">Italie</option>
<option value="sweden">Su&egrave;de</option>
</select>

瑞典的口音(“Suè”)显示不正确(没有对 HTML 代码的解释),而国家的口音(“Contrée”) ) 在浏览器中按预期显示。

为了完成这个,我的法语 django.po 文件就是这样的

...
#: templates/1.html:106
msgid "Country"
msgstr "Contr&eacute;e"

#: templates/1.html:106 myApps/forms.py:19
msgid "Sweden"
msgstr "Su&egrave;de"
...

,就好像当包含在 {{ form.country }} 标记中时,重音的 HTML 代码没有被解释。

关于我做错了什么有什么想法吗? 提前致谢。

I am having troubles using the translation great functionnality of Django when using ChoicesField in a form.

I use a form such as this one :

from django.utils.translation import ugettext as _

class MyForm(forms.Form):
COUNTRIES = (
    ('france', _('France')),
    ('italy', _('Italy')),
    ('sweden', _('Sweden')),
)

country = forms.ChoiceField(choices=COUNTRIES)

Rendered through a view in a template such a this
...

{% trans "Country" %}: {{ form.country }}

...

And my issues are related to accent such as in this example when translated in France :

<label>Contrée </label>
<select name="country" id="id_country">
<option value="france">France</option>
<option value="italy">Italie</option>
<option value="sweden">Suède</option>
</select>

The accent in Sweden ("Suè") is incorrectly displayed (with no interpretation of the HTML code) whereas the one in Country ("Contrée") is displayed as expected in the browser.

To complete this, my django.po file for French is as such

...
#: templates/1.html:106
msgid "Country"
msgstr "Contrée"

#: templates/1.html:106 myApps/forms.py:19
msgid "Sweden"
msgstr "Suède"
...

It is as if the HTML code for accent was not interpreted when included in the {{ form.country }} tag.

Any ideas on what i am doing wrong ?
Thanks in advance.

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

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

发布评论

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

评论(1

慵挽 2024-11-13 03:58:41

我终于解决了这个问题。我没有正确使用我的 django.po 翻译文件。

不必为每个重音字符使用 HTML 代码,而是必须将它们保留为正常(即:é、è、à...),并且 django 将根据目的地相应地翻译这些重音。

这是我的法语翻译标题:

msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2011-06-06 17:17+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: ****** <****@*****.com>\n"
"Language-Team: FR <***@*********.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: Français\n"
"Plural-Forms: nplurals=2; plural=n>1;\n"

希望这可以帮助你们中的一些人。
问候,

I finally solved the issue. I was not using correctly my django.po translation file.

Instead of using HTML code for each accented character, they had to be left normale (ie : é, è, à...) and django would depending on the destination, translate those accents accordingly.

Here is my header for french translation:

msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2011-06-06 17:17+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: ****** <****@*****.com>\n"
"Language-Team: FR <***@*********.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: Français\n"
"Plural-Forms: nplurals=2; plural=n>1;\n"

Hope this can help some of you.
Regards,

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