summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Medvinsky <me@dmedvinsky.name>2015-04-29 20:43:54 +0300
committerTim Graham <timograham@gmail.com>2015-04-29 21:23:22 -0400
commit49dea4164ad9540bcd73ea320e62b1ba054d421b (patch)
treea1d9391d0a027cf02f55ee89242d3f87b9d2e937
parentafae8ff916e85488a31384a681cd4d4fedea58b8 (diff)
[1.8.x] Added translation.override() context manager to docs.
Backport of cf34ee68f0d70cfbcba40524b5d1086ad94874e1 from master
-rw-r--r--docs/topics/i18n/translation.txt11
1 files changed, 11 insertions, 0 deletions
diff --git a/docs/topics/i18n/translation.txt b/docs/topics/i18n/translation.txt
index 3a88add712..3aa36fafd5 100644
--- a/docs/topics/i18n/translation.txt
+++ b/docs/topics/i18n/translation.txt
@@ -1739,6 +1739,7 @@ will affect code running in the same thread.
For example::
from django.utils import translation
+
def welcome_translated(language):
cur_language = translation.get_language()
try:
@@ -1757,6 +1758,16 @@ which returns the language used in the current thread,
for the current thread, and ``django.utils.translation.check_for_language()``
which checks if the given language is supported by Django.
+To help write more concise code, there is also a context manager
+``django.utils.translation.override()`` that stores the current language on
+enter and restores it on exit. With it, the above example becomes::
+
+ from django.utils import tranlations
+
+ def welcome_translated(language):
+ with translation.override(language):
+ return translation.ugettext('welcome')
+
Language cookie
---------------