Permalink
Browse files

Merge pull request #1704 from digi604/cookie-middleware

language cookie middleware added
  • Loading branch information...
digi604 committed Apr 12, 2013
2 parents bcfeac5 + 14dacf2 commit e435c658b61c8b333f7c2463e522ee630fdf211a
View
@@ -136,4 +136,4 @@ Please see Install/2.4 release notes *before* attempting to upgrade to version 2
- CMS_FLAT_URLS has been removed
- CMS_MODERATOR has been removed and replaced with simple publisher.
- PlaceholderAdmin has now language tabs and has support for django-hvad
-
+- Added `cms.middleware.language.LanguageCookieMiddleware`
View
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+import datetime
+
+from django.utils.translation import get_language
+from django.conf import settings
+
+
+class LanguageCookieMiddleware(object):
+ def process_response(self, request, response):
+ language = get_language()
+ if settings.LANGUAGE_COOKIE_NAME in request.COOKIES and \
+ request.COOKIES[settings.LANGUAGE_COOKIE_NAME] == language:
+ return response
+ max_age = 365 * 24 * 60 * 60 # 10 years
+ expires = datetime.datetime.now() + datetime.timedelta(seconds=max_age)
+ response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language, expires=expires.utctimetuple(),
+ max_age=max_age)
+ return response
+
+
View
@@ -63,6 +63,7 @@ def configure(db_url, **extra):
'django.middleware.common.CommonMiddleware',
'django.middleware.transaction.TransactionMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
+ 'cms.middleware.language.LanguageCookieMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
View
@@ -35,6 +35,27 @@ Main `urls.py` example::
.. _documentation: https://docs.djangoproject.com/en/dev/topics/i18n/translation/#internationalization-in-url-patterns
+***************
+Language Cookie
+***************
+
+By default if someone visits a page at `http://www.mysite.fr/` django determines the language as follow:
+
+- language in url
+- language in session
+- language in cookie
+- language in from browser
+- LANGUAGE_CODE from settings
+
+No if i have a German browser and visit a page that is only English and French, it will choose the language that is
+in LANGUAGE_CODE. If this is English but i only speak French i have to switch the language. No if after sometime
+i visit the page again. The page will display in English again and I have to switch again. The same is for every
+link that points to `/` will switch to English again. To fix this behavior the cms ships with a middleware:
+
+ `cms.middleware.language.LanguageCookieMiddleware`
+
+add this to you middleware settings fix this.
+
****************
Language Chooser
****************
@@ -84,3 +105,5 @@ Automated slug generation unicode characters
If your site has languages which use non-ASCII character sets, you might want
to enable :setting:`CMS_UNIHANDECODE_HOST` and :setting:`CMS_UNIHANDECODE_VERSION`
to get automated slugs for those languages too.
+
+
@@ -17,11 +17,16 @@ How django CMS determines the user's preferred language
django CMS determines the user's language the same way Django does it.
* the language code prefix in the URL
-* the last language the user chose in the language chooser (cookie).
+* the language set in the session
+* the language in the `django_language` cookie
* the language that the browser says its user prefers
It uses the django built in capabilities for this.
+By default no session and cookie are set. If you want to enable this use the
+`cms.middleware.language.LanguageCookieMiddleware` to set the cookie on every request.
+
+
How django CMS determines what language to serve
================================================
@@ -140,6 +140,7 @@ at the right position::
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
+ 'cms.middleware.language.LanguageCookieMiddleware',
)
You need at least the following :setting:`django:TEMPLATE_CONTEXT_PROCESSORS`::
@@ -491,5 +492,5 @@ with the different plugins provided out of the box and to build great websites!
.. _South: http://south.aeracode.org/
.. _TinyMCE: http://tinymce.moxiecode.com/
-.. _official documentation: http://docs.djangoproject.com/en/1.2/topics/templates/
+.. _official documentation: http://docs.djangoproject.com/en/1.5/topics/templates/
.. _mailinglist: https://groups.google.com/forum/#!forum/django-cms
View
@@ -177,6 +177,17 @@ What you need to do:
- reverse urls now return the language prefix as well. So maybe there is some code that adds language prefixes. Remove
this code.
+Added LanguageCookieMiddleware
+==============================
+
+To fix the behavior of django to determine the language every time from new, when you visit `/` on a page, this
+middleware saves the current language in a cookie with every response.
+
+To enable this middleware add the following to your `MIDDLEWARE_CLASSES` setting:
+
+ `cms.middleware.language.LanguageCookieMiddleware`
+
+
CMS_LANGUAGES
=============

0 comments on commit e435c65

Please sign in to comment.