搜尋

首頁  >  問答  >  主體

python - django國際化,更改.po檔無效

請教各位大神,由於最近在學Django,學到django國際化的時候,出現了小問題:
無論我怎麼更改django.po文件,刪除重新生成,重啟伺服器,每次顯示的頁面仍然是我第一次使用.po檔的樣子/(ㄒoㄒ)/~~
settings.py如下:

LANGUAGE_CODE = 'zh-CN'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LANGUAGES = (
    ('en', ('English')),
    ('zh_CN', ('中文简体')),
    ('zh-hant', ('中文繁體')),
)
LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)

也加入了對應的中間件:

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

測試的函數,在views.py中如下:

def test1_view(request): 
    n = 2
    weekdays = [_('Monday'), _('Tuesday'), _('Wednesday'), _('Thursday'), _('Friday'), _('Saturday'), 
_('Sunday')] 
    return HttpResponse(weekdays[n])

利用$ python manage.py makemessages -l zh_CN,在/locale/zh_CN/LC_MESSAGES/django.po處產生檔案變更如下:

#: testdeploy/views.py:64
msgid "Monday"
msgstr "一"

#: testdeploy/views.py:64
msgid "Tuesday"
msgstr "二"

#: testdeploy/views.py:64
msgid "Wednesday"
msgstr "三"

#: testdeploy/views.py:64
msgid "Thursday"
msgstr "四"

#: testdeploy/views.py:64
msgid "Friday"
msgstr "五"

#: testdeploy/views.py:64
msgid "Saturday"
msgstr "六"

#: testdeploy/views.py:65
msgid "Sunday"
msgstr "七"

並且也刪除了#,fuzzy,利用$ django-admin.py compilemessages進行了編譯,返回的仍然是第一次試驗的時候的:

#而且,即使我將.po檔案刪除,重啟伺服器同樣可以顯示,請問這是為什麼呢?是否仍然存在類似cookies之類的快取需要清除呢?
謝謝各位~

高洛峰高洛峰2757 天前725

全部回覆(2)我來回復

  • 阿神

    阿神2017-05-18 11:00:02

    經過自強學堂寄給塗老師請教,問題在於將settings.py中設定成

    LANGUAGES = (
        ('en', ('English')),
        ('zh-CN', ('中文简体')),
        ('zh-TW', ('中文繁體')),
    )

    而生成的時候仍然按照下劃線生成,即執行$ python manage.py makemessages -l zh_CN

    回覆
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-18 11:00:02

    python manage.py compilemessages 了麼?

    回覆
    0
  • 取消回覆