Django:ChoiceField 表单中的本地化问题
在表单中使用 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ède</option>
</select>
瑞典的口音(“Suè”)显示不正确(没有对 HTML 代码的解释),而国家的口音(“Contrée”) ) 在浏览器中按预期显示。
为了完成这个,我的法语 django.po 文件就是这样的
...
#: templates/1.html:106
msgid "Country"
msgstr "Contrée"
#: templates/1.html:106 myApps/forms.py:19
msgid "Sweden"
msgstr "Suè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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于解决了这个问题。我没有正确使用我的 django.po 翻译文件。
不必为每个重音字符使用 HTML 代码,而是必须将它们保留为正常(即:é、è、à...),并且 django 将根据目的地相应地翻译这些重音。
这是我的法语翻译标题:
希望这可以帮助你们中的一些人。
问候,
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:
Hope this can help some of you.
Regards,