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.py
as 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~
I sent an email to Teacher Tu for advice through Ziqiang Academy. The problem is that settings.py is set to
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?