From 7ff68253773cfe1902869dcf1374f80eef57abc1 Mon Sep 17 00:00:00 2001 From: Dmitry Medvinsky Date: Wed, 29 Apr 2015 20:43:54 +0300 Subject: [PATCH] [1.7.x] Added translation.override() context manager to docs. Backport of cf34ee68f0d70cfbcba40524b5d1086ad94874e1 from master --- docs/topics/i18n/translation.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/topics/i18n/translation.txt b/docs/topics/i18n/translation.txt index 1ef36a3a12..faaf24be3f 100644 --- a/docs/topics/i18n/translation.txt +++ b/docs/topics/i18n/translation.txt @@ -1678,6 +1678,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: @@ -1696,6 +1697,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 ---------------