Login

Ensure ugettext is available absolutely everywhere

Author:
ElfSternberg
Posted:
June 29, 2009
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

Put this into the init.py file in the root of your project (the same directory level as urls.py and settings.py) and this installs _() as a global reference into the current running python VM, and now it’s as universally available as int(), map(), or str().

This is, of course, controversial. Modifying the python global namespace to add a function can be considered maintenance-hostile. But the gettext feature is so universal– at least to me– that init.py is where it belongs.

1
2
3
4
5
6
# Add to __init.py__ in root of your project (same directory 
# as urls.py and settings.py):

from django.utils.translation import ugettext
import __builtin__
__builtin__.__dict__['_'] = ugettext

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months, 1 week ago
  5. Help text hyperlinks by sa2812 12 months ago

Comments

Please login first before commenting.