Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django LANGUAGE_CODE = 'en' but displays 'fr'?

Tags:

python

django

I wrote my templates in ENGLISH by using {% trans %} inside.

I decided to translate that into French, so I put that in the settings :

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

LANGUAGES = (
    ('en', _('English')),
    ('fr', _('French')),
)

LANGUAGE_CODE = 'fr'

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

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

Then I used makemessages and compilemessages to generated translated strings. And this worked fine ! My website is in French.

But now I would like to get back my site to be in English, So I just change LANGUAGE_CODE to :

LANGUAGE_CODE = 'en'

But my website is still in French ! Even the django-debug-bar is still in French.

The only workaround I found is to comment out the french in LANGUAGES :

LANGUAGES = (
    ('en', _('English')),
    # ('fr', _('French')),
)
  • I tried to move the localMiddleware in the list : no success,
  • I tried to clear cookies : no success.
  • I generated english .mo and .po files : no success
  • I tried LANGUAGE_CODE = 'en-us' : no success

Why LANGUAGE_CODE = 'en' does not work ?

like image 826
Eric Avatar asked Jan 30 '23 07:01

Eric


1 Answers

You need to comment or disable 'django.middleware.locale.LocaleMiddleware', in settings.py.

This middleware handles the choice of the language from the request parameters.


Translation documentation :

https://docs.djangoproject.com/en/1.11/topics/i18n/translation/#how-django-discovers-language-preference

Middleware documentation :

https://docs.djangoproject.com/en/1.11/ref/middleware/#module-django.middleware.locale

like image 92
PRMoureu Avatar answered Feb 08 '23 17:02

PRMoureu