Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django i18n not recognizing language files

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.

like image 305
Hellnar Avatar asked Jul 18 '12 18:07

Hellnar


1 Answers

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.

like image 194
stalk Avatar answered Oct 19 '22 03:10

stalk