1
votes

After looking at the trying all of the recommendations I could find on this subject, my translations still do not work whatsoever.

/settings.py file

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

LOCALE_PATHS = (
    '/Users/samb/Projects/transtest/locale'
)

# Custom Languages
ugettext = lambda s: s

LANGUAGES = (
    ('de', ugettext('German')),
    ('en', ugettext('English')),
    ('fr', ugettext('French')),
    ('fr-CA', ugettext('French Canadian')),
)

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not        
# to load the internationalization machinery.
USE_I18N = True                                                                  

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

view.py

from django.shortcuts import render_to_response
from django.template import RequestContext


def trans(request):
    return render_to_response('index.html', context_instance=RequestContext(request))

my template file (index.html)

{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
<html>
    <head>
        <title>translations</title>
    </head>
    <body>
        {% for l in LANGUAGES %}
            {{ l }}<br />
        {% endfor %}
        {{ LANGUAGE_CODE }}<br />
        {% trans "Welcome to my site" %}
    </body>
</html>

po file (that has been compiled) located at /Users/samb/Projects/transtest/locale/fr/LC_MESSAGES

#: transtest/templates/index.html:13
msgid "Welcome to my site"
msgstr "Please work"

I can never get the 'Welcome to my site' to work. The LANGUAGES and LANGUAGE_CODE variables in my template are all working (unless I 'Accept_language: fr_CA').

After reading all the other posts on this subject and still having the same problem, I feel like I must have a silly mistake, or am missing a vital step entirely. Any thoughts?

Update: This is how I am testing the translation:

telnet localhost 8000
Connected to localhost.
Escape character is '^]'.
GET /
Accept_language: fr
<html>
    <head>
        <title>Translations</title>
    </head>
    <body>

            (&#39;de&#39;, u&#39;Allemand&#39;)<br />

            (&#39;en&#39;, u&#39;Anglais&#39;)<br />

            (&#39;fr&#39;, u&#39;Fran\xe7ais&#39;)<br />

            (&#39;fr-CA&#39;, u&#39;French Canadian&#39;)<br />

        fr<br />
        Welcome to my site
    </body>
</html>
Connection closed by foreign host.

I noticed that the languages are getting translated, but the 'Welcome to my site' is not.

1

1 Answers

1
votes

The docs say here that LocaleMiddleware ...

... should come after SessionMiddleware, because LocaleMiddleware makes use of session data. And it should come before CommonMiddleware because CommonMiddleware needs an activated language in order to resolve the requested URL.

Maybe it will help when you take account of this in your definition of the MIDDLEWARE_CLASSES.