Author: jezdez Date: 2010-03-01 04:19:01 -0600 (Mon, 01 Mar 2010) New Revision: 12624
Modified: django/trunk/django/utils/cache.py django/trunk/docs/topics/cache.txt django/trunk/tests/regressiontests/cache/tests.py Log: Refined changes made in r12546 to also respect the request.LANGUAGE_CODE in case the LocaleMiddleware is used to discover the language preference. Modified: django/trunk/django/utils/cache.py =================================================================== --- django/trunk/django/utils/cache.py 2010-02-28 15:29:14 UTC (rev 12623) +++ django/trunk/django/utils/cache.py 2010-03-01 10:19:01 UTC (rev 12624) @@ -25,7 +25,7 @@ from django.utils.encoding import smart_str, iri_to_uri from django.utils.http import http_date from django.utils.hashcompat import md5_constructor -from django.utils import translation +from django.utils.translation import get_language from django.http import HttpRequest cc_delim_re = re.compile(r'\s*,\s*') @@ -134,6 +134,15 @@ if newheader.lower() not in existing_headers] response['Vary'] = ', '.join(vary_headers + additional_headers) +def _i18n_cache_key_suffix(request, cache_key): + """If enabled, returns the cache key ending with a locale.""" + if settings.USE_I18N: + # first check if LocaleMiddleware or another middleware added + # LANGUAGE_CODE to request, then fall back to the active language + # which in turn can also fall back to settings.LANGUAGE_CODE + cache_key += '.%s' % getattr(request, 'LANGUAGE_CODE', get_language()) + return cache_key + def _generate_cache_key(request, headerlist, key_prefix): """Returns a cache key from the headers given in the header list.""" ctx = md5_constructor() @@ -144,18 +153,14 @@ path = md5_constructor(iri_to_uri(request.path)) cache_key = 'views.decorators.cache.cache_page.%s.%s.%s' % ( key_prefix, path.hexdigest(), ctx.hexdigest()) - if settings.USE_I18N: - cache_key += '.%s' % translation.get_language() - return cache_key + return _i18n_cache_key_suffix(request, cache_key) def _generate_cache_header_key(key_prefix, request): """Returns a cache key for the header cache.""" path = md5_constructor(iri_to_uri(request.path)) cache_key = 'views.decorators.cache.cache_header.%s.%s' % ( key_prefix, path.hexdigest()) - if settings.USE_I18N: - cache_key += ".%s" % translation.get_language() - return cache_key + return _i18n_cache_key_suffix(request, cache_key) def get_cache_key(request, key_prefix=None): """ Modified: django/trunk/docs/topics/cache.txt =================================================================== --- django/trunk/docs/topics/cache.txt 2010-02-28 15:29:14 UTC (rev 12623) +++ django/trunk/docs/topics/cache.txt 2010-03-01 10:19:01 UTC (rev 12624) @@ -323,10 +323,13 @@ .. versionadded:: 1.2 If :setting:`USE_I18N` is set to ``True`` then the generated cache key will -include the name of the currently active :term:`language<language code>`. +include the name of the active :term:`language<language code>`. This allows you to easily cache multilingual sites without having to create the cache key yourself. +See :ref:`topics-i18n-deployment` for more on how Django discovers the active +language. + __ `Controlling cache: Using other headers`_ The per-view cache Modified: django/trunk/tests/regressiontests/cache/tests.py =================================================================== --- django/trunk/tests/regressiontests/cache/tests.py 2010-02-28 15:29:14 UTC (rev 12623) +++ django/trunk/tests/regressiontests/cache/tests.py 2010-03-01 10:19:01 UTC (rev 12624) @@ -476,7 +476,6 @@ ('es', 'Spanish'), ) settings.CACHE_MIDDLEWARE_KEY_PREFIX = 'settingsprefix' - settings.CACHE_MIDDLEWARE_SECONDS self.path = '/cache/test/' def tearDown(self): -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-upda...@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.