- 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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Please login first before commenting.