Babel integration in django
Posted July 16, 2007, 2:50 a.m.
Few months ago I looked for i18n data collections and I found WeBDA. It’s used by Satchmo and it could be easily integrated with other django projects.
I looked also at Babel and I wrote a simple middleware class that allows to have locale data collections available accessing the LOCALE attribute of request objects:
import babel
try:
from threading import local
except ImportError:
from django.utils._threading_local import local
_thread_locals = local()
def get_current_locale():
“””
Get current locale data outside views.
See http://babel.edgewall.org/wiki/ApiDocs/babel.core for Locale
objects documentation
“””
return getattr(_thread_locals, ‘locale’, None)
class BabelMiddleware(object):
“””
This is a very simple middleware that uses
babel (http://babel.edgewall.org) for accessing locale
data in request objects through request.LOCALE attribute
“””
def process_request(self, request):
try:
locale = babel.Locale.parse(request.LANGUAGE_CODE, sep=’-‘)
except (ValueError, babel.UnknownLocaleError):
pass
else:
_thread_locals.locale = locale
setattr(request, ‘LOCALE’, locale)
The get_current_locale function allows also to access locale data outside application views. This middleware configuration must be placed after the ‘django.middleware.locale.LocaleMiddleware’ MIDDLEWARE_CLASSES setting.
MIDDLEWARE_CLASSES = (
… cut …
‘django.middleware.locale.LocaleMiddleware’,
‘middleware.locale.BabelMiddleware’,
… cut …
)











Dec. 30, 2011, 11:44 a.m. by loans
All people deserve wealthy life time and loan or secured loan would make it much better. Just because freedom is based on money state.
March 23, 2012, 10:46 p.m. by personal loans
Houses are quite expensive and not everybody can buy it. However, loans was invented to help people in such hard situations.
Oct. 5, 2012, 9:43 p.m. by Lidia
I was drawn by the hntoesy of what you write
Oct. 8, 2012, 9:37 a.m. by ndmsfrqyof
2QHZ5E <a href="http://cblzatasauux.com/">cblzatasauux</a>
Post a comment