Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #13014 - Added section about how to use the language code with …

…fragement caching. Thanks for the initial patch, fgutierrez.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12787 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3c06e2101bbb05863a5325dc8b6fb1979384f899 1 parent 44390ae
Jannis Leidel authored March 15, 2010
33  docs/topics/cache.txt
@@ -320,6 +320,8 @@ time, rather than ``CACHE_MIDDLEWARE_SECONDS``. Using the decorators in
320 320
 the ``never_cache`` decorator). See the `using other headers`__ section for
321 321
 more on these decorators.
322 322
 
  323
+.. _i18n-cache-key:
  324
+
323 325
 .. versionadded:: 1.2
324 326
 
325 327
 If :setting:`USE_I18N` is set to ``True`` then the generated cache key will
@@ -413,7 +415,9 @@ the ``cache`` template tag. To give your template access to this tag, put
413 415
 
414 416
 The ``{% cache %}`` template tag caches the contents of the block for a given
415 417
 amount of time. It takes at least two arguments: the cache timeout, in seconds,
416  
-and the name to give the cache fragment. For example::
  418
+and the name to give the cache fragment. For example:
  419
+
  420
+.. code-block:: html+django
417 421
 
418 422
     {% load cache %}
419 423
     {% cache 500 sidebar %}
@@ -424,7 +428,9 @@ Sometimes you might want to cache multiple copies of a fragment depending on
424 428
 some dynamic data that appears inside the fragment. For example, you might want a
425 429
 separate cached copy of the sidebar used in the previous example for every user
426 430
 of your site. Do this by passing additional arguments to the ``{% cache %}``
427  
-template tag to uniquely identify the cache fragment::
  431
+template tag to uniquely identify the cache fragment:
  432
+
  433
+.. code-block:: html+django
428 434
 
429 435
     {% load cache %}
430 436
     {% cache 500 sidebar request.user.username %}
@@ -434,10 +440,29 @@ template tag to uniquely identify the cache fragment::
434 440
 It's perfectly fine to specify more than one argument to identify the fragment.
435 441
 Simply pass as many arguments to ``{% cache %}`` as you need.
436 442
 
  443
+If :setting:`USE_I18N` is set to ``True`` the per-site middleware cache will
  444
+:ref:`respect the active language<i18n-cache-key>`. For the ``cache`` template
  445
+tag you could use one of the
  446
+:ref:`translation-specific variables<template-translation-vars>` available in
  447
+templates to archieve the same result:
  448
+
  449
+.. code-block:: html+django
  450
+
  451
+    {% load i18n %}
  452
+    {% load cache %}
  453
+
  454
+    {% get_current_language as LANGUAGE_CODE %}
  455
+
  456
+    {% cache 600 welcome LANGUAGE_CODE %}
  457
+        {% trans "Welcome to example.com" %}
  458
+    {% endcache %}
  459
+
437 460
 The cache timeout can be a template variable, as long as the template variable
438 461
 resolves to an integer value. For example, if the template variable
439 462
 ``my_timeout`` is set to the value ``600``, then the following two examples are
440  
-equivalent::
  463
+equivalent:
  464
+
  465
+.. code-block:: html+django
441 466
 
442 467
     {% cache 600 sidebar %} ... {% endcache %}
443 468
     {% cache my_timeout sidebar %} ... {% endcache %}
@@ -448,6 +473,8 @@ timeout in a variable, in one place, and just reuse that value.
448 473
 The low-level cache API
449 474
 =======================
450 475
 
  476
+.. highlight:: python
  477
+
451 478
 Sometimes, caching an entire rendered page doesn't gain you very much and is,
452 479
 in fact, inconvenient overkill.
453 480
 
2  docs/topics/i18n/internationalization.txt
@@ -385,6 +385,8 @@ used, have in mind that the ``blocktrans`` construct is internally converted
385 385
 to an ``ungettext`` call. This means the same :ref:`notes regarding ungettext
386 386
 variables <pluralization-var-notes>` apply.
387 387
 
  388
+.. _template-translation-vars:
  389
+
388 390
 Each ``RequestContext`` has access to three translation-specific variables:
389 391
 
390 392
     * ``LANGUAGES`` is a list of tuples in which the first element is the

0 notes on commit 3c06e21

Please sign in to comment.
Something went wrong with that request. Please try again.