Login

Other translation block

Author:
cecedille1
Posted:
May 2, 2011
Language:
Python
Version:
1.3
Tags:
i18n mail translation
Score:
1 (after 1 ratings)

Use in a with statement to set the translation to another locale for a block
>>> from django.utils.translation import ugettext >>> ugettext('title') u'title' >>> with Translation('fr') as locale: ...: print locale.locale ...: print ugettext('title') ...:
...:
fr titre >>> ugettext('title') u'title'

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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)

More like this

  1. "Magic Link" Management Command by webology 1 month, 2 weeks ago
  2. Closest ORM models to a latitude/longitude point by simonw 1 month, 2 weeks ago
  3. Log the time taken to execute each DB query by kennyx46 1 month, 2 weeks ago
  4. django database snippet by ItsRLuo 1 month, 3 weeks ago
  5. Serialize a model instance by chriswedgwood 2 months, 2 weeks ago

Comments

Please login first before commenting.