开发者

django form errors in Chinese (or any language apart from Eng?)

I want to know whether开发者_开发百科 form errors can be shown in Chinese or other languages?

This is how I am getting my errors

{{ form.field_name.errors }}


If you set the language in a view, the error messages will be printed in the selected language:

    from django.utils.translation import activate
    activate('de')


Some quick explanation first:

Your form field errors come from

raise forms.ValidationError(_("My Error Message"))

In Django (possibly in other places also), it is common to see this:

from django.utils.translation import ugettext_lazy as _

This turns the _ into a ugettext_lazy, i.e. the code above is REALLY:

raise forms.ValidationError(ugettext_lazy("My Error Message"))

but to avoid typing ugettext_lazy (or your translation function) over and over again, we simply map it to _.

Answer to your question

Finally, to answer your question on whether the errors can be shown in Chinese (or other languages). This depends on whether the validation error message is passed into django.utils.translation.ugettext. In the example above, the "My Error Message" would be displayed in Chinese translated if the user's LANGUAGE_CODE is zh-tw or zh-cn.

Below is an example where there would be no translation:

raise forms.ValidationError("My Error Message")

I highly recommend reading: https://docs.djangoproject.com/en/1.3/topics/i18n/internationalization/ as there is additional work you will need to do to get the translations to show up properly.


if you use _ in your form messages the message will be displayed in the right lang! but if you want to check which lang is running in the template you can use:

{% if LANGUAGE_CODE != "ch" %}

{% endif %}

for more info about using _ in your form, check this out: https://docs.djangoproject.com/en/1.3/ref/forms/validation/#form-subclasses-and-modifying-field-errors

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜