creating a multilingual middleware

20 views
Skip to first unread message

Panos

unread,
Dec 2, 2007, 6:41:47 PM12/2/07
to django-multilingual
Not sure how generic this can be, but DM, could use a multilingual
middleware. When you use the lang code in your url to define the
language of the content, ie:

www.example.com/en/
www.example.com/it/

so you don't need to check for the path each time in your views.

Some work on this has been done by orestis:
http://orestis.gr/en/blog/2007/06/03/international-part4/

yml

unread,
Dec 3, 2007, 6:45:35 AM12/3/07
to django-multilingual
Panos,
I have exactly the same need for one of my project. Making the
language explicit in the url can help in lot of area. Among others
indexation by search engine.
I will look at the link you have proposed.

this is my 2 cts

--yml

Panos Laganakos

unread,
Dec 3, 2007, 10:14:33 AM12/3/07
to django-mu...@googlegroups.com
Indeed, request.path should be considered read-only, but I don't see
any other way to make <lang-id>/ addition transparent to the urls.

--
Panos Laganakos

Marcin Kaszynski

unread,
Dec 3, 2007, 4:46:28 PM12/3/07
to django-multilingual
On Dec 3, 4:14 pm, "Panos Laganakos" <panos.lagana...@gmail.com>
wrote:
> Indeed, request.path should be considered read-only, but I don't see
> any other way to make <lang-id>/ addition transparent to the urls.

Never tried it, but what about a top-level urls.py that looks
something like this:

urlpatterns = patterns('',
(r'^[a-z]+/', include('the-real-urls.py'))
)

It should cut off the leading "[a-z]+/" and leave the rest for further
processing. To make it safer you might specify a list of language
codes instead of [a-z]+, something like this:

urlpatterns = patterns('',
(r'^(?:en|pl|de)/', include('the-real-urls.py'))
)

The ?: should prevent the URL resolver from adding text matched by
expression in parentheses to the list of view args. It is safe to
include settings in urlconf, so you might generate the list from
settings.LANGUAGES automatically.

Might just work :)

-mk

Panos

unread,
Dec 6, 2007, 10:22:17 AM12/6/07
to django-multilingual
Marcin, haven't tried that yet, but we're gonna need a way to call
`set_default_language` depending on the url, it might as well be the
middleware that resolves the urls.

On Dec 3, 11:46 pm, Marcin Kaszynski <marcin.kaszyn...@gmail.com>
wrote:

yml

unread,
Dec 6, 2007, 12:33:11 PM12/6/07
to django-multilingual
Hello,
I also believe that we need some how to mark the xml:lang in the
template. I am new to the concept of multilanguage public web site.
For instance I have a web site http://yml.alwaysdata.com where some
content is in both french and english. But for some reasons google
keep indexing it in english.

Am I on the right track to believe that once urls explicitely state
the language both file version will be indexed?
I am assuming that I am able to produce the proper sitemap.xml. This
should not be a problem.

Thank you for your help

yml

unread,
Dec 14, 2007, 9:26:43 AM12/14/07
to django-multilingual
Hello guys,
I have done a bit more search and I found this:
http://www.jondesign.net/articles/2006/jul/02/langue-depuis-url-django-url-locale-middleware/

This partly solve the issue I was describing, language code in the
URL.
I would be glad if you could let me know what is your opinion about
it.
"""
from django.utils.cache import patch_vary_headers
from django.utils import translation
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)
request.LANGUAGE_CODE = translation.get_language()
def process_response(self, request, response):
patch_vary_headers(response, ("Accept-Language",))
translation.deactivate()
return response
""""

thank you

On 6 déc, 18:33, yml <yann.ma...@gmail.com> wrote:
> Hello,
> I also believe that we need some how to mark the xml:lang in the
> template. I am new to the concept of multilanguage public web site.
> For instance I have a web sitehttp://yml.alwaysdata.comwhere some

eXt

unread,
Dec 16, 2007, 5:54:16 PM12/16/07
to django-multilingual
On 14 Gru, 15:26, yml <yann.ma...@gmail.com> wrote:
> This partly solve the issue I was describing, language code in the
> URL.
Author writes to request.path as Orestis did (look at link in the
first post of this topic).

Have you tried Marcin's suggestion? I mean:

urlpatterns = patterns('',
(r'^(?:en|pl|de)/', include('the-real-urls.py'))
)

Its clean, simple, built-in and... works for me.

> I would be glad if you could let me know what is your opinion about
> it.
Code is rather poor - look at useless: "if lang in supported and lang
is not None: return lang" in the end of first function. Putting
language into session or cookie might be useful sometimes.

My questions
1. Why there is no: response['Content-Language'] =
translation.get_language() in process_response? It is used in
django.middleware.locale.py...

2. What does django-multilingual's set_default_language do? It sets
DEFAULT_LANGUAGE to True but what does it mean? How is it different
from setting request.LANGUAGE_CODE?

One more interesting link:
http://www.djangosnippets.org/snippets/271/

Jakub

Panos Laganakos

unread,
Dec 17, 2007, 8:39:32 AM12/17/07
to django-mu...@googlegroups.com
Maybe a mixture of Marcin's suggestion, and
http://www.djangosnippets.org/snippets/271/ would be the ideal, 'cause
even if we get the lang from the url we need to set the default lang
from the middleware.

--
Panos Laganakos

yml

unread,
Dec 17, 2007, 4:04:38 PM12/17/07
to django-multilingual
Hello guys,
I wrote a small article of the solution I have chosen. It seems to
work fairly well so far I would be glad to hear your feedbacks.
http://yml-blog.blogspot.com/search/label/Internationalisation

Panos,
thank you for the link I will investigate.

--yml

On Dec 17, 2:39 pm, "Panos Laganakos" <panos.lagana...@gmail.com>
wrote:
> Maybe a mixture of Marcin's suggestion, andhttp://www.djangosnippets.org/snippets/271/would be the ideal, 'cause
> even if we get the lang from the url we need to set the default lang
> from the middleware.
>
Reply all
Reply to author
Forward
0 new messages