Getting the language from the URL: locale_url

98 views
Skip to first unread message

Joost Cassee

unread,
May 30, 2008, 10:13:00 AM5/30/08
to django-mu...@googlegroups.com
Hi all,


A followup to my previous message: I have made a Django application that
gets the language from the URL. It is located at:
http://www.cassee.org/trac/wiki/LocaleUrl

To enable, add locale_url.middleware.LocaleUrlMiddleware to settings.py
and add locale_url to the applications list to enable the template tags
and filters. Add the i18n context processor. Adding the request context
processor is also advised to give template tags access to the request.

The app support the following functionality:
* Paths like '/nl/admin/' are rewritten to '/admin/', so urls.py does
not have to be changed.
* Paths without locale are redirected to the default locale (from
settings.LANGUAGE_CODE) or a previous language discovery middleware.
* settings.LANGUAGE_INDEPENDENT_PATHS is a tuple of regular expressions
of paths that should not be redirected. Also MEDIA_URL is not redirected.
* urlresolvers.reverse is monkey patched to return paths with locale.
* 'locale_url' template tag to render a path in HTML (like the 'url'
tag, but with locale info): {% locale_url "nl" app.views.view arg=value
%}, or {% locale_url "de" %} for the current page in a different language.

Bugs:
* The 'reverse' patch does not take LANGUAGE_INDEPENDENT_PATHS into
account (will be fixed soon).
* Admin (in newforms-admin at least) does not use the 'url' tag, but its
own 'app_path' variable. This breaks login. Override 'admin/login.html'
and change the form action URL as follows:

<form action="/{{ LANGUAGE_CODE }}{{ app_path }}" method="post"
id="login-form">

I known this is all still a bit rough. Still, I hope some of you find it
useful. Comments and discussion on design and implementation are very
welcome.


Regards,

Joost

Panos Laganakos

unread,
May 30, 2008, 10:37:52 AM5/30/08
to django-mu...@googlegroups.com
Ah, seems we're getting somewhere with this :)

I'll look into it, thanks for posting it.

--
Panos Laganakos

Panos Laganakos

unread,
Jun 11, 2008, 7:49:21 AM6/11/08
to django-mu...@googlegroups.com
Maybe this should be moved on to the DM repository as it's closely
related to it?

Most people (if not all), who use DM, would want to use this app too.
Plus it'll get much closer testing there.

Any ideas how to resolve the `reverse` issue yet? :)

--
Panos Laganakos

Joost Cassee

unread,
Jun 11, 2008, 8:08:02 AM6/11/08
to django-mu...@googlegroups.com
On 11-06-08 13:49, Panos Laganakos wrote:

> Maybe this should be moved on to the DM repository as it's closely
> related to it?

I am now hosting is on Google Code:

http://django-localeurl.googlecode.com

> Most people (if not all), who use DM, would want to use this app too.
> Plus it'll get much closer testing there.

I agree that it is a good match. The idea of having the language on the
URL has also seen the most discussion on this list. On the other hand it
is definitely functionality that can stand on its own. I like having
small independent apps. You might call it the UNIX school of Django. :-)

It is not that I am adverse to having it in the same repo, it's just
that setting up a Google Code project is so easy that the overhead of
having it in its own place is negligible.

> Any ideas how to resolve the `reverse` issue yet? :)

That issue has been fixed. I now replace the reverse function on one of
my own if the middleware is installed.

The only problem left is the fact that the admin interface does not use
the standard Django standard of generating URLS using reverse and the
url tag. Maybe I should just special-case the admin?


Regards,

Joost

--
Joost Cassee
http://joost.cassee.net

signature.asc
Reply all
Reply to author
Forward
0 new messages