I want to deploy a website in english & spanish and detect the user browser language & redirect to the correct locale site.
My site is www.elmalabarista.com
I install django-localeurl, but I discover that the language is not correctly detected.
This are my middlewares:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'multilingual.middleware.DefaultLanguageMiddleware',
'middleware.feedburner.FeedburnerMiddleware',
'lib.threadlocals.ThreadLocalsMiddleware',
'middleware.url.UrlMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'maintenancemode.middleware.MaintenanceModeMiddleware',
'middleware.redirect.RedirectMiddleware',
'openidconsumer.middleware.OpenIDMiddleware',
'django.middleware.doc.XViewMiddleware',
'middleware.ajax_errors.AjaxMiddleware',
'pingback.middleware.PingbackMiddleware',
'localeurl.middleware.LocaleURLMiddleware',
'multilingual.flatpages.middleware.FlatpageFallbackMiddleware',
'django.middleware.common.CommonMiddleware',
)
But ALWAYS the site get to US despite the fact my OS & Browser setup is spanish.
LANGUAGES = (
('en', ugettext('English')),
('es', ugettext('Spanish')),
)
DEFAULT_LANGUAGE = 1
Then, I hack the middleware of locale-url and do this:
def process_request(self, request):
locale, path = self.split_locale_from_request(request)
if request.META.has_key('HTTP_ACCEPT_LANGUAGE'):
locale = utils.supported_language(request.META['HTTP_ACCEPT_LANGUAGE'].split(',')[0])
locale_path = utils.locale_path(path, locale)
if locale_path != request.path_info:
if request.META.get("QUERY_STRING", ""):
locale_path = "%s?%s" % (locale_path,
request.META['QUERY_STRING'])
return HttpResponseRedirect(locale_path)
request.path_info = path
if not locale:
locale = settings.LANGUAGE_CODE
translation.activate(locale)
request.LANGUAGE_CODE = translation.get_language()
However, this detect fine the language but redirect the "en" urls to "es". So is impossible navigate in english.
UPDATE: This is the final code (after the input from Carl Meyer) with a fix for the case of "/":
def process_request(self, request):
locale, path = self.split_locale_from_request(request)
if (not locale) or (locale==''):
if request.META.has_key('HTTP_ACCEPT_LANGUAGE'):
locale = utils.supported_language(request.META['HTTP_ACCEPT_LANGUAGE'].split(',')[0])
else:
locale = settings.LANGUAGE_CODE
locale_path = utils.locale_path(path, locale)
if locale_path != request.path_info:
if request.META.get("QUERY_STRING", ""):
locale_path = "%s?%s" % (locale_path, request.META['QUERY_STRING'])
return HttpResponseRedirect(locale_path)
request.path_info = path
translation.activate(locale)
request.LANGUAGE_CODE = translation.get_language()