user4591041
user4591041

Reputation:

set_language not working in django internationalization

  1. I included the following in the settings.py:

    LANGUAGES = (
        ('en', 'English'),
        ('ru', 'Russian'),
    )
    
    LANGUAGE_CODE = 'en-us'
    
    USE_I18N = True
    
  2. marked the strings to be translated

    _('Enterprise')     # _ is lazy translate
    
  3. included this in my URLCOnf:

    url(r'^i18n/', include('django.conf.urls.i18n'))
    
  4. created the locale folder and did this:

    python manage.py makemessages -l ru
    
  5. translated the strings and did this:

    python manage.py compilemessages
    
  6. wrote this form:

    <form action="/i18n/setlang/" method="post">
            {% csrf_token %}
            <input name="next" type="hidden" value="/" />
            <select name="language">
               {% for lang in LANGUAGES %}
               <option value="{{ lang.0 }}">{{ lang.1 }}</option>
               {% endfor %}
            </select>
            <input type="submit" value="Translate" />
     </form>
    

I think I did all the steps to make it work but seems like I doing something wrong or missing something.

When use the form and try to translate and print request.LANGUAGE_CODE, it is showing me expected value. But the strings remain in the same language as they were

What is wrong here?

Upvotes: 1

Views: 2112

Answers (2)

Mina Samy
Mina Samy

Reputation: 96

Probably you forgot to add the locale middleware into your settings file it should look like this

'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware', 
'django.middleware.common.CommonMiddleware',

You should take into consideration that the locale middleware should preced the commonmiddleware And comes after the Sessionmiddleware As it takes it argument from the session middleware

Upvotes: 6

GwynBleidD
GwynBleidD

Reputation: 20539

You should define your LOCALE_PATHS in settings.py file like this

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale/'),
)

django doesn't by default look for locale dir in root of project.

Upvotes: 1

Related Questions