views:

84

answers:

2

What is the best way to support multilanguage URLs in Django? Like:

http://myenglishwebsite.com/user/foo
http://mygermanwebsite.com/benutzer/foo

Should I use multilang in urls.py like:

(r'^%s/(?P<slug>[-w]+)/$' % _('user'), 'myapp.view.relatedaction')

It doesn't seem like a good solution and I couldn't make it work :))

A: 

The best way is to just use the same urls for every language. They are not there for the enduser to read anyway.

Have you thought about what will happen when your url cannot be represented by ASCII?

gnibbler
It is important for search engines. So, I want to support that. I'm talking about the ASCII representations/slug versions. Since I can decide that, there won't be any problem.
pocoa
@pocoa, thanks, do you have some links I can read about search engines that need the url to be translated that way?
gnibbler
@gnibbler: Actually, I was looking for that at the moment. Now I'm not sure about that. I was looking at the Lèse majesté's link above. Google says "There's no need to create special URLs when developing a multilingual website." Maybe I'm wrong, maybe I don't really need it. I'm trying to find the answer. Sorry, if I lead you wrong.
pocoa
@gnibbler: I've asked another question about it. Please check http://stackoverflow.com/questions/3306742/multilanguage-urls
pocoa
You should always use slugs not a raw content, then encoding isn't a problem. I don't agree that users don't read urls, I do read them. Search engines also read them and what's more important they highlight search term in urls of found pages, if url contains it.
Łukasz Korzybski
@Łukasz, can you offer some urls so I can read about those search engines?
gnibbler
@gnibbler: http://www.seomoz.org/article/search-ranking-factors#ranking-factors but this is just a survey. I don't argue that this has significance in page ranking, cause I guess nobody knows that for sure. But what is 100% certain is that currently google and bing, both highlight search terms in pages' urls. How important is that, one should answer this himself.
Łukasz Korzybski
+1  A: 

This solution doesn't work because urls.py file is loading once in Django server before first user actually can make any request, so it must be user-independent file (any module level code should be user-independent, because it is loading only once).

My guess is that Django url resolver makes str() casting somewhere in the middle of the request, so you can use some decorator class:

(URLLangDecorator(r'^%s/(?P<slug>[-w]+)/$', ['user']), 'myapp.view.relatedaction')
class URLLangDecorator:
    def __init__(self, url, params):
        self.url, self.params = url, params

    def __str__(self):
        return self.url % map(_, self.params)
    # Django can also preform "%" operation, so to be safe:
    def __mod__(self, arg):
        return str(self) % arg

This is guess, so I'm not sure if it will work.

Tomasz Wysocki
Thanks for your answer. Actually it's going to be two different installations(sites). So I'm not looking for any user dependent solution. All I want to have is a generic solution. So in the future for example I can have a French site. Then I can just change one setting and then it's going to support only French URLs for that site.
pocoa