Houman
Houman

Reputation: 66320

Internationalization in Django doesn't get activated

I have followed the documentation how to do the i18n but the words still show up in English.

Settings.py:

USE_I18N = True
LANGUAGES = (
  ('en', 'English'),
  ('de', 'German'),
)
LANGUAGE_CODE = 'de'

Views:

from django.utils.translation import ugettext as _
...
messages.set_level(request, messages.SUCCESS)
messages.success(request, _(u'An invitation was sent to %s.') % invitation.email)

I am developing on Ubuntu and gettext is installed. In the command line I enter these:

django-admin.py makemessages -l de

I get a .po file and edit it accordingly:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2012-06-01 17:42+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"

#: MyBookmarks/templates/friend_invite.html:3
#: MyBookmarks/templates/friend_invite.html:4
msgid "Invite A Friend"
msgstr "hhhhh"

#: MyBookmarks/templates/friend_invite.html:11
msgid "send invite"
msgstr "gggggggggg"

#: MyBookmarksApp/forms.py:8
msgid "Friend's Name"
msgstr "ffffffffff"

#: MyBookmarksApp/forms.py:9
msgid "Friend's Email"
msgstr "ddddddddd"

#: MyBookmarksApp/views.py:123
#, python-format
msgid "An invitation was sent to %s."
msgstr "ssssssssssss %s"

#: MyBookmarksApp/views.py:127
msgid "An error happened when sending the invitation."
msgstr "aaaaaaa"

Then I run this command:

django-admin.py compilemessages

and the .mo file is created. I run the app and everything is still in plain English. What am I missing?

Update 2:

I have now tried a new approach:

I have added

'django.middleware.locale.LocaleMiddleware',

to the MIDDLEWARE_CLASSES in settings.py

Then I have added to url.py the following:

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

And in my base.html template I have added this:

{% load i18n %}
...
<div id="footer">
    <form action="/i18n/setlang/" method="post">
      <select name="language">
        {% for lang in LANGUAGES %}
          <option value="{{ lang.0 }}">{{ lang.1 }}</option>
        {% endfor %}
      </select>
      <input type="submit" value="Go" />
    </form>
  </div>

Now I can see a dropdown on my page to change the language. Even after changing the language to German, nothing changes. Something seems broken in 1.4...

update 3:

I have created a new simple test project to demonstrate the problem.

It is a very simple project and you can switch between German & English at main page. You see the selected Language code actually changes, which is a good sign but the translation simply doesn't happen. I wonder if this is a bug that needs reported. Your cooperation is highly appreciated.

Please download from here: http://www.chasebot.com/TestProject.zip

(Once extracted in settings.py you need to change the absolute path to the database)

Please let me know if you can reproduce it. Thank you

Upvotes: 1

Views: 2481

Answers (1)

Tisho
Tisho

Reputation: 8482

Do you use the set_language() method ? https://docs.djangoproject.com/en/1.4/topics/i18n/translation/#set-language-redirect-view

How do you change the current language? Using a form ? Are you sure the language is changed ? Print the current language in the html template. It might be some cookie been stored in the browser with non-"DE" value already, preventing the switch to the desired language.

UPDATE: OK, it seems Django cannot find the locale directory. The directory structure seems a bit messy. Read here : https://docs.djangoproject.com/en/dev/topics/i18n/translation/#how-django-discovers-translations

The directories listed in LOCALE_PATHS have the highest precedence, with the ones appearing first having higher precedence than the ones appearing later.

Then, it looks for and uses if it exists a locale directory in each of the installed apps listed in INSTALLED_APPS. The ones appearing first have higher precedence than the ones appearing later.

So, you don't have LOCALE_PATHS in settings.py, and locale/ is not a subdir of an app, included in INSTALLED_APPS.

Just add

LOCALE_PATHS = ('/path/to/locale/', )

This should do the trick (I've tested with your test project).

Upvotes: 5

Related Questions