开发者

modelform field name can't be localized when in production

开发者 https://www.devze.com 2023-02-13 03:04 出处:网络
I have a model: class Server(models.Model): serverId = models.IntegerField(verbose_name=_(\"serverId\"))

I have a model:

class Server(models.Model):
     serverId = models.IntegerField(verbose_name=_("serverId"))
     name = models.CharField(max_length=200, verbose_name=_("server_name"))
     ip = models.CharField(max_length=200, verbose_name=_("ip"))
     cport = models.IntegerField(default=5000, verbose_name=_("cport"))
     aport = models.IntegerField(default=1000, verbose_name=_("aport"))
     hport = models.IntegerField(default=2000, verbose_name=_("hport"))
     version = models.CharField(max_length=100, verbose_name=_("version"))
     serverGroup = models.ForeignKey(Group, null=True, blank=True,
             verbose_name=_('server_group'))
     class Meta:
         db_table = u'server'

     def __unicode__(self):
         return self.name

and the modelform:

class ServerForm(ModelForm):
    class Meta:
        model = Server

from within this app directory I did

$ mkdir locale
$ django-admin.py makemessages -l zh_CN

then I provided the translation in locale/zh_CN/LC_MESSAGES/django.po then I did

$ django-admin.py compilemessages

then I ran the development server:

$ python manage.py runse开发者_StackOverflow社区rver

and went to look at the url http://127.0.0.1:8000 in firefox and the translation displayed. So I thought I did right and I deployed the project on the same machine using nginx + fastcgi with nothing changed in the whole project. Then I go to the url http://127.0.0.1, and then the the modelform shows English there. It didn't localize to Chinese.

I've googled much and read many docs from docs.djangoproject.com and still don't know how to solve the problem. So I ask here.

I only set LANGUAGE_CODE = 'zh_CN' in my settings.py and leave everything on deafult. My django version is 1.2.4

Any of your comments are appreciated.


Make sure you are using lazy_translation. Are you importing ugettext_lazy or ugettext?

from django.utils.translation import ugettext_lazy as _

http://docs.djangoproject.com/en/dev/topics/i18n/internationalization/#lazy-translation


For information, django takes the first argument as the verbose name of a field.

So you can also write your models in a shorter way, like this :

version = models.CharField(_("version"), max_length=100)
serverGroup = models.ForeignKey(_('server_group'), Group, null=True, blank=True)


On version 1.4 it works only (for me) with ugettext not with ugettext_lazy

0

精彩评论

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

关注公众号