django internationalization, changing .po file has no effect
高洛峰
高洛峰 2017-05-18 10:58:02
0
2
736

I would like to ask everyone for advice. Since I have been learning Django recently and learned about the internationalization of django, a small problem has arisen:
No matter how I change the django.po file, delete it and regenerate it, restart the server, the page displayed every time is still the same. This is the first time I used the .po file/(ㄒoㄒ)/~~
settings.pyas follows:

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'),
)

The corresponding middleware has also been added:

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',
)

The tested function is as follows in views.py:

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

Using $ python manage.py makemessages -l zh_CN, the generated file changes at /locale/zh_CN/LC_MESSAGES/django.po are as follows:

#: 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 "七"

and also deleted #, fuzzy, and used $ django-admin.py compilemessages to compile. The result returned is still the one from the first test:

Moreover, even if I delete the .po file, it can still be displayed after restarting the server. Why is this? Are there still caches like cookies that need to be cleared?
Thank you everyone~

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
阿神

I sent an email to Teacher Tu for advice through Ziqiang Academy. The problem is that settings.py is set to

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

When generated, it is still generated according to the underscore, that is, executed $ python manage.py makemessages -l zh_CN

淡淡烟草味

Have you run python manage.py compilemessages?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template