i was thinking about adding some middleware to DM. a middleware that
would ease making multilingual urls.
i found two possible solutions.
1st:
becouse google will not pagerank you high when you will have urls like
domain.com/en/article/1 domainc.com/fr/article1 etc. so some people
use
domain.us/article/1 ,
fr_domain.fr/article/1 etc.
so the solution would look like this:
adding a var to settings.py like:
LANGUAGES_DOMAINS = (
('fr','
http://domain.fr'),# no following slash becouse url adds
it
('en',
http://domain.us'),
)
then there would be possible adding a templatetag that would be used
like:
{% language_domain CURRENT_LANGUAGE_CODE %}{% url view parm="foo" %}
(CURRENT_LANGUAGE_CODE should also be added to context template)
then the middleware would just simply check the domain name and set
the language by the domain.
2nd. (personaly i don't think that is a good idea)
urls should be like /lang/article/1/ =>
domain.com/en/article/1/
(ugly)
so imho middleware is no good (but is aslo usefull) (at the end i will
paste a working code of middleware that actualy works and sets
language modified from some blog i don't remember)
middleware would
lang = (check if there is django_language in session or in cookie.)
if not-> lang= try to select language code from request
if can get language by get_language_id_from_id_or_code() sets current
language = lang
else: sets current language = default_language
anyway it is possible to merge those two solutions by setting
LANGUAGES_DOMAINS = (
('fr','
http://domain.com/fr'),# no following slash becouse url
adds it
('en',
http://domain.com/en'),
)
(so now it will not check by the domain name but by the whole request
url and if it starts with)
the whole point is , i'm not a python programmer , and i don't know
python at all, so if someone care to help and write this middleware,
it would make DM much more usefull.
if you think this is worth mentioning, please give me your opinion
about this issue.
now i will paste a middleware code that uses : domain/en/ and works;]
NOTICE: (middleware should be in dict after session midleware -
becouse cookie does not work )
code:
from django.utils.cache import patch_vary_headers
from django.utils import translation
from multilingual.languages import set_default_language
class MultilingualURLMiddleware:
def get_language_from_request (self,request):
from django.conf import settings
import re
supported = dict(settings.LANGUAGES)
lang = settings.LANGUAGE_CODE[:2]
check = re.match(r"/(\w\w)/.*", request.path)
changed = False
if check is not None:
request.path = request.path[3:]
t = check.group(1)
if t in supported:
lang = t
if hasattr(request, "session"):
request.session["django_language"] = lang
else:
response.set_cookie("django_language", lang)
changed = True
if not changed:
if hasattr(request, "session"):
lang = request.session.get("django_language", None)
if lang in supported and lang is not None:
return lang
else:
lang = request.COOKIES.get("django_language", None)
if lang in supported and lang is not None:
return lang
return lang
def process_request(self, request):
from django.conf import settings
language = self.get_language_from_request(request)
if language is None:
language = settings.LANGUAGE_CODE[:2]
translation.activate(language)
set_default_language(language)
request.LANGUAGE_CODE = translation.get_language()
def process_response(self, request, response):
patch_vary_headers(response, ("Accept-Language",))
translation.deactivate()
return response