Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

language by domain in Django

Is there any braindead simple way to select the language specific version of a page on a Django website based on the domain? (the URLs are and will be the same for both languages, except the language code, so I want to have mysite.com/teachers/manage same as mysite.com/en/teachers/manage (done with 'en' default) and mesito.es/teachers to be same as mesito.es/es/teachers - and no, the urls themselves like 'teachers/manage' will never be translated, so I don't want to configure 2 sets of urls for both languages).

Note: All other solutions I've found imply that I also want to translate the URLs, but I know I will never want to translate the URLs and manae 2 sets of them for this particular website.

Note 2 (forgot to add): Most of the website is actually django-cms based, so django-cms specific solutions would be helpful to...


OK, I got what I wanted with this ugly hack through a redirecting view (but still, isn't there a right AND simple way to do this?...):

## mysite.urls:

urlpatterns = patterns('',
    (r'^$', 'mysite.views.language_router')


## mysite.views:

def language_router(request):
    if request.META['HTTP_HOST'].find('myspanishdomain.com') != -1 \
            and request.META['PATH_INFO'] == '/' :
        return HttpResponseRedirect('http://www.myspanishdomain.com/es/')
    return cms.views.details(request, '')

(site at myenglishdomain.com has the default language english)

like image 663
NeuronQ Avatar asked Jan 25 '26 12:01

NeuronQ


2 Answers

Why don't you write your onw tiny middleware class that you add after the LocaleMiddleware, that simply adds the language, if no language has been identified yet based on the rules that you have explained here in that thread.

like image 172
schacki Avatar answered Jan 29 '26 10:01

schacki


You may want to have a look at Transurlvania as it allows you to map a domain to a language:

MULTILANG_LANGUAGE_DOMAINS = {
    'en': ('www.example-en.com', 'English Site'),
    'fr': ('www.example-fr.com', 'French Site')
}
like image 39
mbaechtold Avatar answered Jan 29 '26 09:01

mbaechtold