# parler.utils.context module

Context managers for temporary switching the language.

class parler.utils.context.smart_override(language_code)

  This is a smarter version of `translation.override` which avoids switching the language if there is no change to make. This method can be used in place of `translation.override`:

      with smart_override(self.get_current_language()):
          return reverse('myobject-details', args=(self.id,))

  This makes sure that any URLs wrapped in `i18n_patterns()` will receive the correct language code prefix. When the URL also contains translated fields (e.g. a slug), use `switch_language` instead.

class parler.utils.context.switch_language(object, language_code=None)

  A contextmanager to switch the translation of an object.

  It changes both the translation language, and object language temporary.

  This context manager can be used to switch the Django translations to the current object language. It can also be used to render objects in a different language:

      with switch_language(object, 'nl'):
          print object.title

  This is particularly useful for the `get_absolute_url()` function. By using this context manager, the object language will be identical to the current Django language.

      def get_absolute_url(self):
          with switch_language(self):
              return reverse('myobject-details', args=(self.slug,))

  Note

  When the object is shared between threads, this is not thread-safe. Use `safe_translation_getter()` instead to read the specific field.