Django i18n无法识别语言文件 [英] Django i18n not recognizing language files
问题描述
在Django 1.4中,我想在我的网站上有两种语言,土耳其语('tr')和英语('en')。
On Django 1.4, I want to have two languages in my site, turkish('tr') and english('en').
这是我目前的设置:
settings.py:
settings.py:
USE_I18N = True
LANGUAGES = (
('en', 'English'),
('tr', 'Turkish'),
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware', # I have locale middleware
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
LOCALE_PATHS = (
'/myproject/locale/',
)
我有我的区域设置文件这样的目录顺序:
And I have my locale files as such directory order:
#tr files
/myproject/locale/tr/LC_MESSAGES/django.mo
/myproject/locale/tr/LC_MESSAGES/django.po
#en files
/myproject/locale/en/LC_MESSAGES/django.mo
/myproject/locale/en/LC_MESSAGES/django.po
我仍然看不到我的翻译,尝试从我的shell(它也不适用于{%transCorporate%})的模板:
And I still cannot see my translations, trying from my shell (it also doesn't work for templates as {% trans "Corporate" %}):
>>> from django.utils import translation
>>> translation.activate('tr')
>>> translation.ugettext('Corporate')
u'Corporate'
?奇怪的是,它将像Home这样的单词翻译成原始的Django具有这些但不是我的翻译文件的翻译。
Am I missing anything here? Weirdly, it translates for words like 'Home' as original Django has translations for these but not my translation files.
推荐答案
在您的情况下,您必须将 LOCALE_PATHS
绝对路径地区目录,即
In your case you must put into LOCALE_PATHS
absolute path to your locale directory, i.e.
LOCALE_PATHS = (
'/home/path_to_your_project/myproject/locale/',
)
我想你已经尝试设置相对路径。
I guess you've tried to set relative path there.
我有类似的问题:我一直在使用django 1.3,我的locale目录是我的项目的根目录,即在settings.py和manage.py文件附近。
但是当我用django 1.4创建项目时,项目目录结构已被更改:settings.py已经移动到myproject / myproject文件夹中。我仍然在myproject /(不在myproject / myproject)中创建 locale
文件夹。和django 1.4它不工作了。
I had similiar problem: i've been using django 1.3 and my locale directory was in the root of my project, i.e. near settings.py and manage.py files.
But when i create project with django 1.4, project directory structure have been changed: settings.py have moved into myproject/myproject folder. I still create locale
folder in myproject/ (not in myproject/myproject). And with django 1.4 it is not working any more.
阅读文档我明白,django找不到我的 locale
文件夹。所以帮助我的解决方案 - 将 locale
dir移动到myproject / myproject中,不要设置任何 LOCALE_PATHS
或者离开 locale
dir in myproject / path,并在 LOCALE_PATHS
元组中的settings.py中添加完整路径。
Reading documentation i understand, that django just can't find my locale
folder. So solution that helps me - either move locale
dir into myproject/myproject and don't set any LOCALE_PATHS
OR leave locale
dir in myproject/ path and add full path to it in settings.py in LOCALE_PATHS
tuple.
这篇关于Django i18n无法识别语言文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!