Browse files

contextmanager for force_language

  • Loading branch information...
1 parent a6f90d1 commit 203125ea6e0a39e0bfdbfee66c8cd4be754f28a5 @digi604 committed Oct 3, 2012
Showing with 7 additions and 10 deletions.
  1. +7 −10 cms/utils/i18n.py
View
17 cms/utils/i18n.py
@@ -1,17 +1,14 @@
# -*- coding: utf-8 -*-
+from contextlib import contextmanager
from django.conf import settings
from django.utils import translation
-class force_language(object):
- def __init__(self, new_lang):
- self.new_lang = new_lang
- self.old_lang = translation.get_language()
-
- def __enter__(self):
- translation.activate(self.new_lang)
-
- def __exit__(self, type, value, tb):
- translation.activate(self.old_lang)
+@contextmanager
+def force_language(new_lang):
+ old_lang = translation.get_language()
+ translation.activate(new_lang)
+ yield
+ translation.activate(old_lang)
def get_language_list(site_id=None):

0 comments on commit 203125e

Please sign in to comment.