4

settings.py

from django.utils.translation import ugettext_lazy as _
LANGUAGE_CODE = 'hi'
# list of activated languages
LANGUAGES = (
    ('hi', _('Hindi')),
    ('en', _('English')),
)

urls.py

urlpatterns += i18n_patterns(
    url(r'^', include('howdy.urls')),
    url(r'^', include('accounts.urls')),
    url(r'^admin/', admin.site.urls),
    url(r'^i18n/', include('django.conf.urls.i18n')),
    url(r'^accounts/', include('allauth.urls'))
)

Middleware

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

So the default I set here is "hi" , but whenever I am hitting the URL[private mode] without appending any language in it, its redirecting with "en" in the url, not the "hi". What I am missing here to make it default point to "hi" language ?

Dimag Kharab
  • 4,439
  • 1
  • 24
  • 45

2 Answers2

3

Are you using LocaleMiddleware? See: How Django discovers language preference:

LocaleMiddleware tries to determine the user’s language preference by following this algorithm:

  • First, it looks for the language prefix in the requested URL. This is only performed when you are using the i18n_patterns function in your root URLconf. See Internationalization: in URL patterns for more information about the language prefix and how to internationalize URL patterns.

  • Failing that, it looks for the LANGUAGE_SESSION_KEY key in the current user’s session.

  • Failing that, it looks for a cookie. The name of the cookie used is set by the LANGUAGE_COOKIE_NAME setting. (The default name is django_language.)

  • Failing that, it looks at the Accept-Language HTTP header. This header is sent by your browser and tells the server which language(s) you prefer, in order by priority. Django tries each language in the header until it finds one with available translations.

  • Failing that, it uses the global LANGUAGE_CODE setting.

Udi
  • 29,222
  • 9
  • 96
  • 129
1

Locale Middleware fetches the language code for the response in the following order:

  • URL Language Prefix (/en/,/hi/) when using i18n_patterns in the urlpatterns
  • django.conf.global_settings.LANGUAGE_COOKIE_NAME cookie in the request object
  • Accept-Language HTTP header
  • LANGUAGE_CODE setting

If the user hits / and language cookie isn't set on the browser, then LocaleMiddleware uses the first compatible language in the Accept-Language header. That is the source of bugs in this case.

The solutions that can work are

  1. Remove the LocaleMiddleware if it doesn't affect your development.
  2. Create a custom middleware that sits before the LocaleMiddleware and sets a key in the request.COOKIES dictionary. It's a bit hacky but it works. And you can set the language cookie when you're sending a response and it would be saved on the browser. To make sure you don't override user's settings you can check if session's keys to check if the language code (django.utils.translation.LANGUAGE_SESSION_KEY) is there.
MD Mushfirat Mohaimin
  • 1,966
  • 3
  • 10
  • 22
user477620
  • 11
  • 2