开发者

Django : Localization issue in ChoiceField forms

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 "开发者_StackOverflowCountry" %}: {{ 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&egrave;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&eacute;e"

#: templates/1.html:106 myApps/forms.py:19
msgid "Sweden"
msgstr "Su&egrave;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.


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,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜