Albert Stepanyan
Albert Stepanyan

Reputation: 1

How to redirect page to user with user prefered language when opening a browser

Django 1.9.6 i18n works perfect when I open the browser and change language. But I want to save language and the other times when user comes it will be in his/her prefered language. Now when I open the site it always redirect me to /en .I tried to change my settings and set

 LANGUAGE_CODE='ru'

or something else but it does not work here is my settings file

   ALLOWED_HOSTS = []
   LOGIN_URL=reverse_lazy('login')
   AUTH_USER_MODEL='custom.MyUser'
   INSTALLED_APPS = [
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'custom',
     'languages',
        ]

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

   ROOT_URLCONF = 'builtcustom.urls'
     WSGI_APPLICATION = 'builtcustom.wsgi.application'
     LANGUAGES=(
                ('en',_('ENGLISH')),
                ('fr',_('FRENCH')),
                ('cs', _('czech')),
                ('hy', _('ARMENIAN')),
                ('ru', _('RUSSIAN')),
                )
    LOCALE_PATHS = (
                        os.path.join(BASE_DIR, 'locale'),

                    )

 LANGUAGE_CODE = 'en-us'
 TIME_ZONE = 'UTC'
 USE_I18N = True
 USE_L10N = True
 USE_TZ =False 

How can i solve this problem and redirect to url with a language that I want(Dynamically selected from database)??

Upvotes: 0

Views: 89

Answers (1)

Aakash
Aakash

Reputation: 1515

For a particular url you an select language like this in Django.

from django.utils.translation import activate
activate('fr') # To use french

Checkout the documentation.
You can easily get whatever language you want from the database and call activate on it. Make sure your the languages in database has same abbreviations as in Django.

Upvotes: 1

Related Questions