Permalink
Browse files

add ``localizer`` property to the request (refs #508)

  • Loading branch information...
1 parent 4448c35 commit c614ffc153135eb14d77472a46c20af7c6f6ed91 @witsch witsch committed Aug 17, 2013
Showing with 35 additions and 10 deletions.
  1. +3 −0 CHANGES.txt
  2. +7 −10 pyramid/i18n.py
  3. +8 −0 pyramid/request.py
  4. +17 −0 pyramid/tests/test_i18n.py
View
@@ -4,6 +4,9 @@ Next Release
Features
--------
+- Add ``localizer`` property (reified) to the request.
+ See https://github.com/Pylons/pyramid/issues/508.
+
- Add ``pdistreport`` script, which prints the Python version in use, the
Pyramid version in use, and the version number and location of all Python
distributions currently installed.
View
@@ -197,17 +197,15 @@ def make_localizer(current_locale_name, translation_directories):
def get_localizer(request):
""" Retrieve a :class:`pyramid.i18n.Localizer` object
corresponding to the current request's locale name. """
- localizer = getattr(request, 'localizer', None)
- if localizer is None:
- # no locale object cached on request
- try:
- registry = request.registry
- except AttributeError:
- registry = get_current_registry()
+ # no locale object cached on request
+ try:
+ registry = request.registry
+ except AttributeError:
+ registry = get_current_registry()
- current_locale_name = get_locale_name(request)
- localizer = registry.queryUtility(ILocalizer, name=current_locale_name)
+ current_locale_name = get_locale_name(request)
+ localizer = registry.queryUtility(ILocalizer, name=current_locale_name)
if localizer is None:
# no localizer utility registered yet
@@ -216,7 +214,6 @@ def get_localizer(request):
registry.registerUtility(localizer, ILocalizer,
name=current_locale_name)
- request.localizer = localizer
return localizer
View
@@ -24,6 +24,7 @@
)
from pyramid.decorator import reify
+from pyramid.i18n import get_localizer
from pyramid.response import Response
from pyramid.url import URLMethodsMixin
from pyramid.util import InstancePropertyMixin
@@ -383,6 +384,13 @@ def is_response(self, ob):
def json_body(self):
return json.loads(text_(self.body, self.charset))
+ @reify
+ def localizer(self):
+ """ Convenience property to return a localizer by calling
+ :func:`pyramid.i18n.get_localizer`. """
+ return get_localizer(self)
+
+
def route_request_iface(name, bases=()):
# zope.interface treats the __name__ as the __doc__ and changes __name__
# to None for interfaces that contain spaces if you do not pass a
View
@@ -292,6 +292,23 @@ def test_locale_from_mo_bad_mo(self):
self.assertEqual(result.translate('Approve', 'deformsite'),
'Approve')
+ def test_request_has_localizer(self):
+ from pyramid.threadlocal import get_current_registry
+ from pyramid.interfaces import ILocalizer
+ from pyramid.request import Request
+ # register mock localizer
+ dummy = object()
+ registry = get_current_registry()
+ registry.registerUtility(dummy, ILocalizer, name='en')
+ request = Request(environ={})
+ self.assertEqual(request.localizer, dummy)
+ # `get_localizer` is only called once...
+ other = object()
+ registry.registerUtility(other, ILocalizer, name='en')
+ self.assertNotEqual(request.localizer, other)
+ self.assertEqual(request.localizer, dummy)
+
+
class Test_default_locale_negotiator(unittest.TestCase):
def setUp(self):
cleanUp()

0 comments on commit c614ffc

Please sign in to comment.