diff --git a/CHANGELOG.txt b/CHANGELOG.txt index e5e60007d89..d48578df23f 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -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` diff --git a/cms/middleware/language.py b/cms/middleware/language.py new file mode 100644 index 00000000000..8063f53d436 --- /dev/null +++ b/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 + + diff --git a/cms/test_utils/cli.py b/cms/test_utils/cli.py index 541cafea063..d0d1dc3fcc3 100644 --- a/cms/test_utils/cli.py +++ b/cms/test_utils/cli.py @@ -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', diff --git a/docs/advanced/i18n.rst b/docs/advanced/i18n.rst index b9b73e4aa6f..a188a44f42e 100644 --- a/docs/advanced/i18n.rst +++ b/docs/advanced/i18n.rst @@ -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. + + diff --git a/docs/concepts/multiple_languages.rst b/docs/concepts/multiple_languages.rst index 8f500a6c5fd..0872a439a63 100644 --- a/docs/concepts/multiple_languages.rst +++ b/docs/concepts/multiple_languages.rst @@ -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 ================================================ diff --git a/docs/upgrade/2.4.rst b/docs/upgrade/2.4.rst index 0e5cd953b31..16eb65e0acb 100644 --- a/docs/upgrade/2.4.rst +++ b/docs/upgrade/2.4.rst @@ -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 =============