Django i18n不工作 [英] Django i18n does not work

查看:158
本文介绍了Django i18n不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目激活不同的语言。英语和西班牙语现在。

I'm trying to activate different languages for my project. English and spanish right now.

我会描述我遵循的所有步骤:

I'll describe all steps i follow:

最重要的是,我将自己放在我要翻译的目录中。或者说更好的说,所有的{%trans%}标签都是:

FIrst of all, i place myself in the directory i want to translate. Or better said, where all {% trans %} tags are:

$ cd media/templates/landing/
$ mkdir locale
$ django-admin.py makemessages --locale=en

最后一个命令创建目录/文件/locale/en/LC_MESSAGES/django.po

Last command creates the directory/file /locale/en/LC_MESSAGES/django.po

我打开django.po,我继续完成所有的英文语言的msgstr字段。 msgid标签是西班牙文。我尊重有关长消息的提示。填写此文件后,我会:

I open django.po and i proceed to complete all msgstr fields in english language. msgid label is in spanish. I respect the tips about long messages case. After fill in this file, i make:

$ django-admin.py compilemessages

此进程django.po并创建django.mo。

This process django.po and creates django.mo.

所以我修改设置。 PY。重要的行:

So i modify settings.py. Important lines:

TEMPLATE_CONTEXT_PROCESSORS = (
    'ism.context_processor.user_vars',
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.contrib.messages.context_processors.messages',
    'django.core.context_processors.request',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'pipeline.middleware.MinifyHTMLMiddleware',
}

TIME_ZONE = 'Atlantic/Canary'

LANGUAGE_CODE = 'es'

USE_I18N = True

_ = lambda s: s

LANGUAGES = (
    ('es', _('Espanol')),
    ('en', _('English')),
)

USE_L10N = True

USE_TZ = True

最后,我将此行添加到URLS.py:

Finally, i add this line to URLS.py:

(r'^i18n/', include('django.conf.urls.i18n')),

我重新启动我的开发服务器,我配置我的Firefox浏览器首先选择英语作为主要语言,它不起作用。所有文本仍然以西班牙语显示英文。

I restart my development server, i configure my Firefox browser to choose English first as primary language and it does not work. All texts still showing in spanish instead english.

我确定Firefox是用英文配置的,因为在Django视图功能(渲染.html)中,我打印请求.LANGUAGE_CODE,打印en。

I make sure Firefox is configured in english because in Django view function (which render the .html) i make a print with request.LANGUAGE_CODE, which prints "en".

我错了什么?

推荐答案

编者注:此答案已从编辑迁移到问题。它是由原始海报写的。

采取区域设置路径时出现问题。 Django在根路径中忽略(或者没有找到)我的locale目录,所以django.po / mo不可用。可以在settings.py中添加LOCALE_PATHS来解决所有这些问题。

There was a problem taking the locale path. Django was ignoring (or just it didn't find) my locale directory in root path so django.po/mo was not available. All this shit stuff can be solved adding LOCALE_PATHS in settings.py.

更多信息 - > https://docs.djangoproject.com/en/dev/topics/i18n/translation/#how-django-discovers-翻译

这篇关于Django i18n不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆