Skip to content

Commit 3c06e21

Browse files
committed
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
1 parent 44390ae commit 3c06e21

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

docs/topics/cache.txt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ time, rather than ``CACHE_MIDDLEWARE_SECONDS``. Using the decorators in
320320
the ``never_cache`` decorator). See the `using other headers`__ section for
321321
more on these decorators.
322322

323+
.. _i18n-cache-key:
324+
323325
.. versionadded:: 1.2
324326

325327
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
413415

414416
The ``{% cache %}`` template tag caches the contents of the block for a given
415417
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
417421

418422
{% load cache %}
419423
{% cache 500 sidebar %}
@@ -424,7 +428,9 @@ Sometimes you might want to cache multiple copies of a fragment depending on
424428
some dynamic data that appears inside the fragment. For example, you might want a
425429
separate cached copy of the sidebar used in the previous example for every user
426430
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
428434

429435
{% load cache %}
430436
{% cache 500 sidebar request.user.username %}
@@ -434,10 +440,29 @@ template tag to uniquely identify the cache fragment::
434440
It's perfectly fine to specify more than one argument to identify the fragment.
435441
Simply pass as many arguments to ``{% cache %}`` as you need.
436442

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+
437460
The cache timeout can be a template variable, as long as the template variable
438461
resolves to an integer value. For example, if the template variable
439462
``my_timeout`` is set to the value ``600``, then the following two examples are
440-
equivalent::
463+
equivalent:
464+
465+
.. code-block:: html+django
441466

442467
{% cache 600 sidebar %} ... {% endcache %}
443468
{% cache my_timeout sidebar %} ... {% endcache %}
@@ -448,6 +473,8 @@ timeout in a variable, in one place, and just reuse that value.
448473
The low-level cache API
449474
=======================
450475

476+
.. highlight:: python
477+
451478
Sometimes, caching an entire rendered page doesn't gain you very much and is,
452479
in fact, inconvenient overkill.
453480

docs/topics/i18n/internationalization.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ used, have in mind that the ``blocktrans`` construct is internally converted
385385
to an ``ungettext`` call. This means the same :ref:`notes regarding ungettext
386386
variables <pluralization-var-notes>` apply.
387387

388+
.. _template-translation-vars:
389+
388390
Each ``RequestContext`` has access to three translation-specific variables:
389391

390392
* ``LANGUAGES`` is a list of tuples in which the first element is the

0 commit comments

Comments
 (0)