新建local
在settings.py加
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LANGUAGES = (
('en', ('English')),
('zh-hans', ('中文简体')),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
...
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.locale.LocaleMiddleware', # 添加此行
]
image.png
终端:
python manage.py makemessages -l zh_hans#中文
python manage.py makemessage en #英文
python manage.py makemessages -l zh_hant #繁体
view.py
from django.utils.translation import ugettext as _
![image.png](https://img.haomeiwen.com/i15578594/5c69e09b6c39dcee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在这个文件里面自行设置翻译(msgstr)
image.pngDjango将自动搜索所有的.po文件,将它们都翻译成.mo文件。
python manage.py compilemessages
image.png
about.py(第一行{% load i18n %})
image.png
网友评论