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. Markdown and Syntax Highlighting in Django by blinks 8 years, 4 months ago
  2. Showell markup--DRY up your templates by showell 5 years, 8 months ago
  3. PostgreSQL fulltext with language translations by diverman 4 years, 11 months ago
  4. Render specific blocks from templates (useful for AJAX) by sciyoshi 7 years, 2 months ago
  5. blocksetter by jerzyk 7 years, 7 months ago

Comments

Please login first before commenting.