adding languages to dashboard menu

29 views
Skip to first unread message

NENAD CIKIC

unread,
Mar 24, 2012, 10:39:24 AM3/24/12
to django-admin-tools
Hello!
I want to add the language menu.
I have started with the following code that works:
{% get_available_languages as LANGUAGES %}
{% get_current_language as LANGUAGE_CODE %}
<form action="/favxp/i18n/setlang/" method="post"> {%
csrf_token %}
<select name="language">
{% for code, desc in LANGUAGES %}
<option value="{{ code }}"
{% ifequal code LANGUAGE_CODE %}
selected="1"
{% endifequal %}>{{ desc }}</option>
{% endfor %}
</select>
<input type="submit" value="Go" />
</form>
I have put that code in base.html and it changes correctly the
presentation languages.

Now I want to get rid of the above and to use the menu instead. So I
have extended the admin_tools items.py as:
class JezikList(MenuItem, AppListElementMixin):
def __init__(self, title=None, **kwargs):
super(JezikList, self).__init__(title, **kwargs)

def init_with_context(self, context):
jezici=[]
for lang in settings.LANGUAGES:
jezici.append(lang[1])

jezici.sort()
for jezik in jezici:
self.children.append(MenuItem(title=jezik,url='/favxp/i18n/
setlang/'))

and in my CustomMenu I have added the following children
items.JezikList(_('Presentation languages'))

The menu works, I mean i see all the languages sorted in the menu, but
nothing happens when i choose one of the languages. I have used the
livehttpheader to monitor what gets generated with the working code,
and it seems i have to add csrfmiddlewaretoken and the language code
as the content.

I am sure i am missing something basic:)
thanks

NENAD CIKIC

unread,
Mar 24, 2012, 1:10:04 PM3/24/12
to django-admin-tools
I have managed to find a workaround (or maybe the correct solution, i
do not know).
I have created ad-hoc view just for changing the language:
def promijeni_jezik(request, jezik_id):
if not request.user.is_authenticated():
raise Http404(unicode(_('Not logged!')))
translation.activate(jezik_id)
request.LANGUAGE_CODE = translation.get_language()
return HttpResponseRedirect(request.META.get('HTTP_REFERER',
reverse('admin:index')))

and items.py becomes:
for lang,desc in settings.LANGUAGES:
jezici.append((lang,desc))

jezici.sort((lambda x, y: cmp(x[1], y[1])))

for jezik in jezici:
self.children.append(MenuItem(title=jezik[1],url='/
promijeni_jezik/'+jezik[0]))

The next step is to change the localemiddleware and to add to the user
profile the last language setting;but i do not expect here any
problems.

Anyway, since i am django newbie, I would be interested in knowing if
I could have done language change in some other pretier way.
Thanks
Reply all
Reply to author
Forward
0 new messages