Kaydet (Commit) 49dea416 authored tarafından Dmitry Medvinsky's avatar Dmitry Medvinsky Kaydeden (comit) Tim Graham

[1.8.x] Added translation.override() context manager to docs.

Backport of cf34ee68 from master
üst afae8ff9
...@@ -1739,6 +1739,7 @@ will affect code running in the same thread. ...@@ -1739,6 +1739,7 @@ will affect code running in the same thread.
For example:: For example::
from django.utils import translation from django.utils import translation
def welcome_translated(language): def welcome_translated(language):
cur_language = translation.get_language() cur_language = translation.get_language()
try: try:
...@@ -1757,6 +1758,16 @@ which returns the language used in the current thread, ...@@ -1757,6 +1758,16 @@ which returns the language used in the current thread,
for the current thread, and ``django.utils.translation.check_for_language()`` for the current thread, and ``django.utils.translation.check_for_language()``
which checks if the given language is supported by Django. 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 Language cookie
--------------- ---------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment