Last active
December 17, 2015 13:59
-
-
Save mkoistinen/5621021 to your computer and use it in GitHub Desktop.
Redirects requests that are not prefixed with a language segment to the right content in the default language. Also, if a URL is encountered that has a language prefix when it shouldn't, it will remove it. This is originally based on http://ilian.i-n-i.org/language-redirects-for-multilingual-sites-with-django-cms/ but modified to support django-…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import re | |
from django.conf import settings | |
from django.http import HttpResponseRedirect | |
from django.utils import translation | |
from django.utils.translation import get_language_from_request | |
# | |
# Originally from: http://ilian.i-n-i.org/language-redirects-for-multilingual-sites-with-django-cms/ | |
# | |
# Modified to support django-CMS 2.4.1 and be a little smarter about redirects | |
# when it appears that a language-segment is present, even if its not a valid | |
# one. | |
# | |
class CustomMultilingualURLMiddleware(object): | |
def process_request(self, request): | |
parts = request.path.split('/', 3) # E.g., '/en-us/front/page/' | |
first_segment = parts[1] # E.g., 'en-us' | |
if first_segment in settings.URLS_WITHOUT_LANGUAGE_REDIRECT: | |
# | |
# This URL doesn't have a language-prefix and doesn't require one, | |
# so, let it ride. | |
# | |
return None | |
if re.match(r'^[a-z]{2}(\-[a-z]{2})?$', first_segment, re.IGNORECASE): | |
# | |
# The first_segment looks like a language pattern. Assume that | |
# the user is attempting to view content in a language that does | |
# not exist, or no longer exists or has a typo in the language | |
# segment. Redirect to the default language of the same content. | |
# | |
if len(parts) > 2 and parts[2] in settings.URLS_WITHOUT_LANGUAGE_REDIRECT: | |
# | |
# URL probably has a language-segment when there shouldn't | |
# even be one, let it ride. Other middleware will deal with | |
# this and if we do, we could cause a redirect loop. (This | |
# needs further testing) | |
# | |
return None | |
else: | |
# | |
# Construct the url without the language segment because we'll | |
# prepend the correct language segment below. | |
# | |
remainder = '/' + '/'.join(parts[2:]) # E.g., '/front/page/' | |
else: | |
# | |
# The first segment does not look like a language path, so, we'll | |
# want to redirect to the whole request.path with the default | |
# language-segment prepended, this will be added below. | |
# | |
remainder = request.path | |
language = get_language_from_request(request) | |
translation.activate(language) | |
request.LANGUAGE_CODE = language | |
if first_segment == '': | |
# | |
# This should be a very common case; the user requested the root | |
# URL of the website '/'. | |
# | |
return HttpResponseRedirect('/%s/' % language) | |
if first_segment not in [code for code, name in settings.LANGUAGES]: | |
# | |
# The language segment refers to a language that we do not | |
# support, so, redirect to the same content, but for the default | |
# language. | |
# | |
return HttpResponseRedirect('/%s%s' % (language, remainder)) | |
# | |
# This is just made explicit, since this is (hopefully) the most | |
# common case. Essentially, at this point, the url has a valid | |
# language segment for a URL that should have one, so, we let it ride. | |
# | |
return None |
I added middleware in my project root and when i added in
MIDDLEWARE_CLASSES
so url
127.0.0.1:8000/fr/
works perfectly
but when i changed language to english
127.0.0.1:8000/en/
so it gives
"Page not found (404)"
Eror
In my settings.py
LANGUAGE_CODE = 'fr'
how can l solve this
Pls help,
Thanks in Advanced!!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TAKE NOTE: This middleware shouldn't even be necessary, and in fact, when I rearranged my projects middleware, the need for this functionality disappeared as it is already done in other middleware.