How to find out why Django ignores the Accept-Language header?

I have a Django application (in Google App Engine) that I want to internationalize.

settings.py:

USE_I18N = True
LANGUAGE_CODE = 'en'

# Restrict supported languages (and JS media generation)
LANGUAGES = (
  ('en', 'English'),
  ('fr', 'French'),
)

MIDDLEWARE_CLASSES = (
  'ragendja.middleware.ErrorMiddleware',
  'django.contrib.sessions.middleware.SessionMiddleware',
  # i18n
  'django.middleware.locale.LocaleMiddleware',
  ...

I created .po and .mo files for my application in the / fr / LC _MESSAGES locale (although not globally).

I set the Accept-Language browser header to "fr" and Django ignores it. When I look at request.LANGUAGE_CODE, it is always "en".

I can say that the browser is right because I visit some other site with i18n support and it returns French.

How to find out what Django is missing in my setup?

I saw this question and it did not help me.

I am running Django 1.0 using the engine patch 1.0.2.2 application in Google App Engine.

+3
2

, Django i18n.

LANGUAGE_CODE. , , , .

-, LocaleMiddleware, , django_language . , .

-, , cookie django_language (, , cookie LANGUAGE_COOKIE_NAME). cookie.

-, Accept-Language HTTP. .

!

+3

, HTTP_ACCEPT_LANGUAGE LocaleMiddleware:

class ForceDefaultLanguageMiddleware(object):
    """
    Ignore Accept-Language HTTP headers

    This will force the I18N machinery to always choose settings.LANGUAGE_CODE
    as the default initial language, unless another one is set via sessions or cookies

    Should be installed *before* any middleware that checks request.META['HTTP_ACCEPT_LANGUAGE'],
    namely django.middleware.locale.LocaleMiddleware
    """

    def process_request(self, request):
        if request.META.has_key('HTTP_ACCEPT_LANGUAGE'):
            del request.META['HTTP_ACCEPT_LANGUAGE']
0

Source: https://habr.com/ru/post/1721642/


All Articles