ioeric ioeric - 7 months ago 27
Python Question

How to change Django's default label name of comment form into Chinese?

I am using Django's built-in comments app. But I have met one problem.

The default label for the comments form is English(e.g. "Name","Email Address"), but I'd like to change them into Chinese. So I went to

site-packages/django/contrib/comments/forms.py
and amend them as below:

name = forms.CharField(label=_("姓名 Name"), max_length=50)
email = forms.EmailField(label=_("邮箱 Email"))
url = forms.URLField(label=_("网站 URL"), required=False)
comment = forms.CharField(label=_("评论 Comment"), widget=forms.Textarea,
max_length=COMMENT_MAX_LENGTH)


But it failed when I runserver again, it says

SyntaxError: Non-ASCII character '\xe5' in file /usr/lib/python2.7/site-packages/django/contrib/comments/forms.py on line 98, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details


So I went to http://www.python.org/peps/pep-0263.html and Add "# -- coding: utf-8 --" in the head of forms.py file. To be joy, I can run the server. But when I went to the comment Page, the comment form disappeared!

Can anybody tell me what's going on? And how to repair it ?

Answer

Django's comments app is already translated to Chinese, you don't need to do that yourself.

You will just need to enable internationalization in your django configuration:

USE_I18N = True

LANGUAGES = (
  ('zh_CN', 'Chinese'),
)

You can roll back all the changes you made to the django/contrib/comments/forms.py file.

Comments