Login

Other translation block

Author:
cecedille1
Posted:
May 2, 2011
Language:
Python
Version:
1.3
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. Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 3 weeks, 4 days ago
  2. Python Django CRUD Example Tutorial by tuts_station 1 month, 1 week ago
  3. Browser-native date input field by kytta 2 months, 3 weeks ago
  4. Generate and render HTML Table by LLyaudet 3 months ago
  5. My firs Snippets by GutemaG 3 months, 1 week ago

Comments

Please login first before commenting.