【问题标题】:django i18n does not use my .mo filesdjango i18n 不使用我的 .mo 文件
【发布时间】:2015-06-24 17:11:56
【问题描述】:

我正在尝试将我的 django 应用程序国际化,但我遇到了困难。

我尝试翻译一些字符串以进行测试,它成功了,但现在即使我编辑 .po 文件并编译创建 .mo 文件,它仍然像第一个测试一样翻译,第一个测试值在第一个测试字符串上.不知道为什么,找了好几天。

这是我的 settings.py 中间件部分:

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    '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',
    'django.middleware.locale.LocaleMiddleware',
)

上下文处理器部分:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.core.context_processors.i18n',
            ],
        },
    },
]

和国际化部分:

LANGUAGE_CODE = 'en-gb'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LOCAL_PATHS = (
    os.path.join(BASE_DIR, 'locale/'), #BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)

LANGUAGES = (
    ('fr', _('French')),
    ('en', _('English')),
)

在我的项目的根目录下有一个“locale”目录,(我已经尝试将绝对路径放在我的 LOCAL_PATHS 中)。

.po 文件由“my/project/locale/lang[en or fr]/LC_MESSAGES/django.po”处的“python manage.py makemessages -l en -l fr -e djhtml -e py”生成,并且.mo 文件由 'my/project/locale/lang[en or fr]/LC_MESSAGES/django.mo' 中的“python manage.py compilemessages”生成。

我尝试清理缓存,重新启动服务器(runserver 的事情),但它仍然没有考虑对 .po 文件的任何修改。

谢谢你帮助我。

【问题讨论】:

    标签: python django internationalization django-i18n


    【解决方案1】:

    它是 LOCALE_PATHS,而不是本地 (link)。

    【讨论】:

    • @lemour_a 没问题。祝你好运。你需要它来处理翻译:)
    猜你喜欢
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多