from django.utils.translation import get_language,activate,check_for_language
from django.conf import settings

class Translation():
    def __init__(self,locale):
        self.old_locale = get_language()
        if locale and locale in dict(settings.LANGUAGES) and check_for_language(locale):
            self.locale=locale
        else:
            self.locale=self.old_locale

    def __enter__(self):
        activate(self.locale)

    def __exit__(self,exc_type, exc_val, exc_tb):
        activate(self.old_locale)