2010年1月25日月曜日

Djangoでi18nize

$cd .../Projects/mysite
$mkdir locale
$django-admin.py makemessages -l ja
$gedit ./locale/ja/LC_MESSAGES/django.po
$django-admin.py compilemessages

1)model.py
from django.db import models
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

# Create your models here.
class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __unicode__(self):
            return self.question
    def was_published_today(self):
        return self.pub_date.date() == datetime.date.today()
    class Meta:
        verbose_name = _('Poll')
        verbose_name_plural = _('Polls')

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()
    def __unicode__(self):
        return self.choice
    class Meta:
        verbose_name = _('Choice')
        verbose_name_plural = _('Choices')

admin.site.register(Poll)
admin.site.register(Choice)

2)setting.py
LANGUAGE_CODE = 'ja-jp'
gettext = lambda s: s
LANGUAGES = (
    ('ja', gettext('Japan')),
    ('en', gettext('English')),
)

3)django.po
#: polls/models.py:14
msgid "Poll"
msgstr "投票"

#: polls/models.py:15
msgid "Polls"
msgstr "投票"

#: polls/models.py:24
msgid "Choice"
msgstr "選択"

#: polls/models.py:25
msgid "Choices"
msgstr "選択"

P.S. msgfmt --statistics -o django.mo django.po

0 件のコメント:

コメントを投稿