change i18n language through hyperlinks

1,023 views
Skip to first unread message

Chuck Bai2

unread,
Oct 9, 2008, 11:51:17 PM10/9/08
to django...@googlegroups.com
I want to allow user select language preference through clicking the
language hyperlinks like "_English_ | _French_" switch. And I also want
to redirect back to the previous page after clicking the language
switch. Is it possible to use Django view:
django.views.i18n.set_language? In the documentation, there is an
example using form submit to select language using this view.

<form action="/i18n/setlang/" method="post">
<input name="next" type="hidden" value="/next/page/" />
<select name="language">
{% for lang in LANGUAGES %}
<option value="{{ lang.0 }}">{{ lang.1 }}</option>
{% endfor %}
</select>
<input type="submit" value="Go" />
</form>


But I want to implement this using hyperlinks and want to know how.


Malcolm Tredinnick

unread,
Oct 10, 2008, 12:30:33 AM10/10/08
to django...@googlegroups.com

You'll need to write your own view to handle this (you could look at
Django's view for inspiration). Django intentionally doesn't support
this because it's bad practice (using a GET request to initiate a state
change).

Regards,
Malcolm

cschand

unread,
Oct 10, 2008, 1:11:17 AM10/10/08
to Django users
Even then you want in get method you can use the below code.

In view

from django import http
from django.utils.translation import check_for_language
from django.conf import settings

def set_language(request, lang_code):
"""
Patched for Get method
"""
next = request.REQUEST.get('next', None)
if not next:
next = '/'
response = http.HttpResponseRedirect(next)
if lang_code and check_for_language(lang_code):
if hasattr(request, 'session'):
request.session['django_language'] = lang_code
else:
response.set_cookie(settings.LANGUAGE_COOKIE_NAME,
lang_code)
return response

And in template
<ul class="language_holder">

{% for lang in LANGUAGES %}

<a href="/setlang/{{ lang.0 }}/">{{ lang.1 }}</a>&nbsp;&nbsp;

{% endfor %}

</ul>

in urls
url(r'^setlang/(?P<lang_code>.*)/$', 'utils.views.set_language')

I dont know it is a good practice to do

Satheesh

On Oct 10, 9:30 am, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:

Chuck22

unread,
Oct 12, 2008, 12:12:58 AM10/12/08
to Django users
thanks Satheesh, the set_language function is partially working for
me. When I click the lauguage hyperlinks, only the word "Home" works,
but when I change the msgid to be "Home1", it stop working. It seems
that "Home" is a django preserved word which get auto-translated.

1. In my base.html, I have

{% load i18n %}

{% trans "Home1" %}

2. django.po and django.mo files are generated correctly for the
lauguages.

msgid "Home1"
msgstr "My_Translation"

3. In my settings.py, I have

USE_I18N = True

LANGUAGE_CODE = 'en-us'

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
)

4. syncdb is done, and session table is generated.

5. language preferrence selection is like the code below.

I don't know which piece is missing. Anyone can suggest? Thanks.
> > Malcolm- Hide quoted text -
>
> - Show quoted text -

Ozgur Odabasi

unread,
Dec 2, 2008, 11:56:05 PM12/2/08
to Django users
try this

def set_language(request, lang_code):
next = request.REQUEST.get('next', None)
if not next:
next = '/'
response = http.HttpResponseRedirect(next)
if lang_code and check_for_language(lang_code):
settings.LANGUAGE_CODE=lang_code
return response

Ozgur Odabasi

unread,
Dec 3, 2008, 4:44:16 AM12/3/08
to Django users
wow this is dangerous!

Changes language settings for all users ;P
But I couldnt unerstand how session and cookie frameworks works,
documented examples doesn't work...
Reply all
Reply to author
Forward
0 new messages