Getting the language from the URL: locale_url

16 views
Skip to first unread message

Joost Cassee

unread,
Jun 2, 2008, 8:25:11 AM6/2/08
to Django users
Hi all,


About two years ago atlithorn posted some code to get the language for
a page from the URL. I have extended it and made it into a Django
application. Basically, it allows you to have URLs like 'http://
www.example.com/nl/about/". It is located at:

http://www.cassee.org/trac/wiki/LocaleUrl

That webpage also has complete docs. I am looking for comments on two
coding decision I made.

1) Monkey patching urlresolvers.reverse

I wanted to be able to use existing applications without having to
change their urls.py module. So the middleware strips off the language
prefix before handing it to the URL resolver. However, this means that
the urlresolvers.reverse function (and the url template tag) will not
return the correct URL. To solve this I let my application monkey
patch (replace) the urlresolvers.reverse function. Is there a better
way?

2) Forcing explicit re.comple('/path/') entries in
LOCALE_INDEPENDENT_PATHS

This settings variable is a tuple holding a list of paths that are not
language dependent. They are regular expressions and because they will
be used often they should not be compiled at every request. Because
the settings variables should not be changed (even at compile time, I
guess) I am now forcing the user to specify them explicitly as regular
expression objects. This looks a bit ugly in settings.py. (See the
example on the webpage.) Is there a better way?


The application currently only works for language specification on the
URL path, not for example the hostname (for example: http://fr.example/com/about/).
I might add this functionality if anyone asks.

I hope this is useful. Any comments, bug, discussion, etc. are very
welcome.


Regards,

Joost
Reply all
Reply to author
Forward
0 new messages