Skip to content

Commit

Permalink
cookie middleware added
Browse files Browse the repository at this point in the history
  • Loading branch information
digi604 committed Apr 10, 2013
1 parent bcfeac5 commit c34025f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.txt
Expand Up @@ -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`
16 changes: 16 additions & 0 deletions cms/middleware/language.py
@@ -0,0 +1,16 @@
# -*- 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):
max_age = 365 * 24 * 60 * 60 # 10 years
expires = datetime.datetime.now() + datetime.timedelta(seconds=max_age)
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, get_language(), expires=expires.utctimetuple(),
max_age=max_age)
return response


1 change: 1 addition & 0 deletions cms/test_utils/cli.py
Expand Up @@ -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',
Expand Down
23 changes: 23 additions & 0 deletions docs/advanced/i18n.rst
Expand Up @@ -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
****************
Expand Down Expand Up @@ -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.


7 changes: 6 additions & 1 deletion docs/concepts/multiple_languages.rst
Expand Up @@ -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
================================================

Expand Down
11 changes: 11 additions & 0 deletions docs/upgrade/2.4.rst
Expand Up @@ -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
=============

Expand Down

0 comments on commit c34025f

Please sign in to comment.