Change language on login

85 views
Skip to first unread message

Mishen'ka

unread,
Feb 21, 2011, 3:50:18 PM2/21/11
to Django users
Hello all,

I have make a site with translated menu items etc.
Now i want to change the language on login.

By example i'm from the netherlands and want the Dutch language.
or i'm from america, so i want English as language.

So i have change few things in the login pagina,
but it doesn't change my language.

Does someone know the solution ?

The login page:


======================
@csrf_protect
@never_cache
def login(request, template_name='registration/login.html',
redirect_field_name=REDIRECT_FIELD_NAME,
authentication_form=AuthenticationForm):
"""Displays the login form and handles the login action."""

redirect_to = request.REQUEST.get(redirect_field_name, '')

if request.method == "POST":
form = authentication_form(data=request.POST)
if form.is_valid():
# Light security check -- make sure redirect_to isn't garbage.


if not redirect_to or ' ' in redirect_to:
redirect_to = settings.LOGIN_REDIRECT_URL

# Heavier security check -- redirects to http://example.com
should
# not be allowed, but things like /view/?param=http://
example.com
# should be allowed. This regex checks if there is a '//'
*before* a
# question mark.
elif '//' in redirect_to and re.match(r'[^\?]*//', redirect_to):
redirect_to = settings.LOGIN_REDIRECT_URL

# Okay, security checks complete. Log the user in.
auth_login(request, form.get_user())
logging.warn(11)
#if lang_code and check_for_language(lang_code):
if hasattr(request, 'session'):
request.session['django_language'] = 'ru'
else:
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, 'ru')
if request.session.test_cookie_worked():
request.session.delete_test_cookie()

return HttpResponseRedirect(redirect_to)

else:
form = authentication_form(request)

request.session.set_test_cookie()

current_site = get_current_site(request)

return render_to_response(template_name, {
'form': form,
redirect_field_name: redirect_to,
'site': current_site,
'site_name': current_site.name,
}, context_instance=RequestContext(request))

======================

Thanks

Tomasz Zieliński

unread,
Feb 21, 2011, 5:28:51 PM2/21/11
to Django users


On 21 Lut, 21:50, "Mishen'ka" <maarikbennietb...@gmail.com> wrote:
> Hello all,
>
> I have make a site with translated menu items etc.
> Now i want to change the language on login.
>
> By example i'm from the netherlands and want the Dutch language.
> or i'm from america, so i want English as language.
>
> So i have change few things in the login pagina,
> but it doesn't change my language.
>
> Does someone know the solution ?
>

Try using LocaleMiddleware if you don't use it at the moment:
http://code.djangoproject.com/browser/django/tags/releases/1.2.3/django/middleware/locale.py

You can also manually pick active locale with
django.utils.translation.activate() function.

--
Tomasz Zielinski

Mishen'ka

unread,
Feb 21, 2011, 5:52:36 PM2/21/11
to Django users
Hi Thomasz,

I have it working with your solution.
Thank you very much!

On 21 feb, 23:28, Tomasz Zieliński <tomasz.zielin...@pyconsultant.eu>
wrote:
> On 21 Lut, 21:50, "Mishen'ka" <maarikbennietb...@gmail.com> wrote:
>
> > Hello all,
>
> > I have make a site with translated menu items etc.
> > Now i want to change the language on login.
>
> > By example i'm from the netherlands and want the Dutch language.
> > or i'm from america, so i want English as language.
>
> > So i have change few things in the login pagina,
> > but it doesn't change my language.
>
> > Does someone know the solution ?
>
> Try using LocaleMiddleware if you don't use it at the moment:http://code.djangoproject.com/browser/django/tags/releases/1.2.3/djan...

galgal

unread,
Jun 21, 2011, 3:30:41 PM6/21/11
to django...@googlegroups.com
Which solution did You use?
Reply all
Reply to author
Forward
0 new messages