Django国际化的坑

最近一个项目要用到国际化,按照官方教程配置始终不生效,最后发现是文件夹名字的原因

settings.py

MIDDLEWARE = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',  # 排在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.security.SecurityMiddleware',
    'wagtail.core.middleware.SiteMiddleware',
    'wagtail.contrib.redirects.middleware.RedirectMiddleware',
]

USE_I18N = True
LANGUAGES = [
    ('en', 'English'),
    ('zh-hans', '中文简体'),
]
LOCALE_PATHS = [
    os.path.join(BASE_DIR, 'locale'),
]

注意 Mac默认不区分大小写,Linux区分 文件夹名字应该是 zh_Hans

locale
└── zh_Hans
    └── LC_MESSAGES
        ├── django.mo
        └── django.po
作者: Lerry
发表时间:2018-06-30