1

I can't figure why the django translation (i18n) doesn't work. I use english in the code and I've written translations for french. Strangely, it works when I use django.test.client.Client but not when I use a web browser.

I've tried to set the Accept-Language to fr in my browser settings and I've also tried to delete the key HTTP_ACCEPT_LANGUAGE from the request header.

I've set the language to fr through the setlang view found in django.conf.urls.i18n.

When I do:

{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}

I successfully get fr, even if the string that should be converted in my page is still in english.

I have this in my settings:

USE_I18N = True
#Probably ignored because of the django_language cookie
LANGUAGE_CODE = 'fr'
MIDDLEWARE_CLASSES = (
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.transaction.TransactionMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
)

I've compiled my translation and they work since I get the expected output when using the test client.

I'm pretty sure the problem is something that the browser send to django that affects the translation, but I can't figure what. I've compared the two and the only references to a language are fr in both version.

Anyone have any suggestion on what I could try?

I did more reseach with the debugger and I found that the catalog in _active doesn't contain my translations when using the browser, but does when using the test client. What could cause that behavior?

1 Answers1

0

Unfortunatelly, while 2016 is coming soon, adding Accept-Language header inside test client still not work. But you can activate language manually. See a code below:

from django.utils.translation import activate
activate('fr')

More details you can read here.

Hope this helps!

Albert Tugushev
  • 1,504
  • 13
  • 25