Django i18n not recognizing language files

╄→гoц情女王★ 提交于 2019-12-22 09:00:10

问题


On Django 1.4, I want to have two languages in my site, turkish('tr') and english('en').

This is my current setup:

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

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'

Am I missing anything here? Weirdly, it translates for words like 'Home' as original Django has translations for these but not my translation files.


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/11547690/django-i18n-not-recognizing-language-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!