summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2011-02-13 11:44:59 -0500
committerChris McDonough <chrism@plope.com>2011-02-13 11:44:59 -0500
commit0ce5a3dedf846df20b44ae559b36407093e79805 (patch)
treed5143692c12e0bda771222642f9f607d17e54753
parent296ee25ffb1405b7a5a12caf62fe5b1ed2aaed7b (diff)
parent2242e6f858de86cedb943091c91ae78b4de6eb5e (diff)
downloadpyramid-0ce5a3dedf846df20b44ae559b36407093e79805.tar.gz
pyramid-0ce5a3dedf846df20b44ae559b36407093e79805.tar.bz2
pyramid-0ce5a3dedf846df20b44ae559b36407093e79805.zip
Merge branch 'lambacck-master'
-rw-r--r--CHANGES.txt3
-rw-r--r--CONTRIBUTORS.txt2
-rw-r--r--docs/api/i18n.rst2
-rw-r--r--docs/narr/i18n.rst3
-rw-r--r--pyramid/i18n.py51
-rw-r--r--pyramid/tests/test_asset.py10
-rw-r--r--pyramid/tests/test_i18n.py37
-rw-r--r--pyramid/tests/test_integration.py2
-rw-r--r--pyramid/tests/test_mako_templating.py16
9 files changed, 90 insertions, 36 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index 160f28bff..9a78dcbee 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -28,6 +28,9 @@ Features
- ``pyramid_alchemy`` paster template now has unit tests.
+- Added ``pyramid.i18n.make_localizer`` API (broken out from
+ ``get_localizer`` guts).
+
1.0 (2011-01-30)
================
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index 9828f7144..dba39d630 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -126,3 +126,5 @@ Contributors
- John Shipman, 2011/01/15
- Wichert Akkerman, 2011/01/19
+
+- Christopher Lambacehr, 2011/02/12
diff --git a/docs/api/i18n.rst b/docs/api/i18n.rst
index 0c2f8b86e..53e8c8a9b 100644
--- a/docs/api/i18n.rst
+++ b/docs/api/i18n.rst
@@ -24,6 +24,8 @@
.. autofunction:: default_locale_negotiator
+ .. autofunction:: make_localizer
+
See :ref:`i18n_chapter` for more information about using
:app:`Pyramid` internationalization and localization services within
an application.
diff --git a/docs/narr/i18n.rst b/docs/narr/i18n.rst
index e7a4f61c0..218b7a2b4 100644
--- a/docs/narr/i18n.rst
+++ b/docs/narr/i18n.rst
@@ -508,6 +508,9 @@ negotiator is registered.
def aview(request):
locale = get_localizer(request)
+.. note:: If you need to create a localizer for a locale use the
+ :func:`pyramid.i18n.make_localizer` function.
+
.. index::
single: translating (i18n)
diff --git a/pyramid/i18n.py b/pyramid/i18n.py
index ee4a693da..4b8281673 100644
--- a/pyramid/i18n.py
+++ b/pyramid/i18n.py
@@ -145,6 +145,33 @@ def get_locale_name(request):
request.locale_name = locale_name
return locale_name
+def make_localizer(current_locale_name, translation_directories):
+ """ Create a :class:`pyramid.i18n.Localizer` object
+ corresponding to the provided locale name from the
+ translations found in the list of translation directories."""
+ translations = Translations()
+ translations._catalog = {}
+ for tdir in translation_directories:
+ locale_dirs = [ (lname, os.path.join(tdir, lname)) for lname in
+ os.listdir(tdir) ]
+ for locale_name, locale_dir in locale_dirs:
+ if locale_name != current_locale_name:
+ continue
+ messages_dir = os.path.join(locale_dir, 'LC_MESSAGES')
+ if not os.path.isdir(os.path.realpath(messages_dir)):
+ continue
+ for mofile in os.listdir(messages_dir):
+ mopath = os.path.realpath(os.path.join(messages_dir,
+ mofile))
+ if mofile.endswith('.mo') and os.path.isfile(mopath):
+ mofp = open(mopath, 'rb')
+ domain = mofile[:-3]
+ dtrans = Translations(mofp, domain)
+ translations.add(dtrans)
+
+ return Localizer(locale_name=current_locale_name,
+ translations=translations)
+
def get_localizer(request):
""" Retrieve a :class:`pyramid.i18n.Localizer` object
corresponding to the current request's locale name. """
@@ -162,29 +189,9 @@ def get_localizer(request):
if localizer is None:
# no localizer utility registered yet
- translations = Translations()
- translations._catalog = {}
tdirs = registry.queryUtility(ITranslationDirectories, default=[])
- for tdir in tdirs:
- locale_dirs = [ (lname, os.path.join(tdir, lname)) for lname in
- os.listdir(tdir) ]
- for locale_name, locale_dir in locale_dirs:
- if locale_name != current_locale_name:
- continue
- messages_dir = os.path.join(locale_dir, 'LC_MESSAGES')
- if not os.path.isdir(os.path.realpath(messages_dir)):
- continue
- for mofile in os.listdir(messages_dir):
- mopath = os.path.realpath(os.path.join(messages_dir,
- mofile))
- if mofile.endswith('.mo') and os.path.isfile(mopath):
- mofp = open(mopath, 'rb')
- domain = mofile[:-3]
- dtrans = Translations(mofp, domain)
- translations.add(dtrans)
-
- localizer = Localizer(locale_name=current_locale_name,
- translations=translations)
+ localizer = make_localizer(current_locale_name, tdirs)
+
registry.registerUtility(localizer, ILocalizer,
name=current_locale_name)
request.localizer = localizer
diff --git a/pyramid/tests/test_asset.py b/pyramid/tests/test_asset.py
index 494838d59..260e4994e 100644
--- a/pyramid/tests/test_asset.py
+++ b/pyramid/tests/test_asset.py
@@ -40,7 +40,7 @@ class TestOverrideProvider(unittest.TestCase):
here = os.path.dirname(os.path.abspath(__file__))
expected = open(os.path.join(here, resource_name)).read()
result = provider.get_resource_stream(None, resource_name)
- self.assertEqual(result.read(), expected)
+ self.assertEqual(result.read().replace('\r', ''), expected)
def test_get_resource_string_no_overrides(self):
import os
@@ -50,7 +50,7 @@ class TestOverrideProvider(unittest.TestCase):
here = os.path.dirname(os.path.abspath(__file__))
expected = open(os.path.join(here, resource_name)).read()
result = provider.get_resource_string(None, resource_name)
- self.assertEqual(result, expected)
+ self.assertEqual(result.replace('\r', ''), expected)
def test_has_resource_no_overrides(self):
resource_name = 'test_asset.py'
@@ -276,7 +276,7 @@ class TestPackageOverrides(unittest.TestCase):
po.overrides= overrides
here = os.path.dirname(os.path.abspath(__file__))
expected = open(os.path.join(here, 'test_asset.py')).read()
- self.assertEqual(po.get_stream('whatever').read(), expected)
+ self.assertEqual(po.get_stream('whatever').read().replace('\r', ''), expected)
def test_get_string(self):
import os
@@ -287,7 +287,7 @@ class TestPackageOverrides(unittest.TestCase):
po.overrides= overrides
here = os.path.dirname(os.path.abspath(__file__))
expected = open(os.path.join(here, 'test_asset.py')).read()
- self.assertEqual(po.get_string('whatever'), expected)
+ self.assertEqual(po.get_string('whatever').replace('\r', ''), expected)
def test_has_resource(self):
overrides = [ DummyOverride(None), DummyOverride(
@@ -434,7 +434,7 @@ class Test_asset_spec_from_abspath(unittest.TestCase):
def test_abspath_startswith_package_path(self):
import os
- abspath = os.path.dirname(__file__) + '/fixtureapp'
+ abspath = os.path.join(os.path.dirname(__file__), 'fixtureapp')
pkg = DummyPackage('pyramid.tests')
pkg.__file__ = 'file'
result = self._callFUT(abspath, pkg)
diff --git a/pyramid/tests/test_i18n.py b/pyramid/tests/test_i18n.py
index 12cf185b4..ce36c57c8 100644
--- a/pyramid/tests/test_i18n.py
+++ b/pyramid/tests/test_i18n.py
@@ -137,6 +137,43 @@ class Test_get_locale_name(unittest.TestCase):
self.assertEqual(result, 'bogus')
self.assertEqual(request.locale_name, 'bogus')
+class Test_make_localizer(unittest.TestCase):
+ def setUp(self):
+ cleanUp()
+
+ def tearDown(self):
+ cleanUp()
+
+ def _callFUT(self, locale, tdirs):
+ from pyramid.i18n import make_localizer
+ return make_localizer(locale, tdirs)
+
+ def test_locale_from_mo(self):
+ import os
+ from pyramid.i18n import Localizer
+ here = os.path.dirname(__file__)
+ localedir = os.path.join(here, 'localeapp', 'locale')
+ localedirs = [localedir]
+ locale_name = 'de'
+ result = self._callFUT(locale_name, localedirs)
+ self.assertEqual(result.__class__, Localizer)
+ self.assertEqual(result.translate('Approve', 'deformsite'),
+ 'Genehmigen')
+ self.assertEqual(result.translate('Approve'), 'Approve')
+ self.failUnless(hasattr(result, 'pluralize'))
+
+ def test_locale_from_mo_bad_mo(self):
+ import os
+ from pyramid.i18n import Localizer
+ here = os.path.dirname(__file__)
+ localedir = os.path.join(here, 'localeapp', 'locale')
+ localedirs = [localedir]
+ locale_name = 'be'
+ result = self._callFUT(locale_name, localedirs)
+ self.assertEqual(result.__class__, Localizer)
+ self.assertEqual(result.translate('Approve', 'deformsite'),
+ 'Approve')
+
class Test_get_localizer(unittest.TestCase):
def setUp(self):
cleanUp()
diff --git a/pyramid/tests/test_integration.py b/pyramid/tests/test_integration.py
index 2f010dc77..a504db982 100644
--- a/pyramid/tests/test_integration.py
+++ b/pyramid/tests/test_integration.py
@@ -61,7 +61,7 @@ class TestStaticApp(unittest.TestCase):
result = staticapp(context, request)
self.assertEqual(result.status, '200 OK')
self.assertEqual(
- result.body,
+ result.body.replace('\r', ''),
open(os.path.join(here, 'fixtures/minimal.pt'), 'r').read())
class IntegrationBase(unittest.TestCase):
diff --git a/pyramid/tests/test_mako_templating.py b/pyramid/tests/test_mako_templating.py
index bec29fc10..8a03d4b5c 100644
--- a/pyramid/tests/test_mako_templating.py
+++ b/pyramid/tests/test_mako_templating.py
@@ -307,45 +307,45 @@ class TestIntegration(unittest.TestCase):
def test_render(self):
from pyramid.renderers import render
- result = render('helloworld.mak', {'a':1})
+ result = render('helloworld.mak', {'a':1}).replace('\r','')
self.assertEqual(result, u'\nHello föö\n')
def test_render_from_fs(self):
from pyramid.renderers import render
self.config.add_settings({'reload_templates': True})
- result = render('helloworld.mak', {'a':1})
+ result = render('helloworld.mak', {'a':1}).replace('\r','')
self.assertEqual(result, u'\nHello föö\n')
def test_render_inheritance(self):
from pyramid.renderers import render
- result = render('helloinherit.mak', {})
+ result = render('helloinherit.mak', {}).replace('\r','')
self.assertEqual(result, u'Layout\nHello World!\n')
def test_render_inheritance_pkg_spec(self):
from pyramid.renderers import render
- result = render('hello_inherit_pkg.mak', {})
+ result = render('hello_inherit_pkg.mak', {}).replace('\r','')
self.assertEqual(result, u'Layout\nHello World!\n')
def test_render_to_response(self):
from pyramid.renderers import render_to_response
result = render_to_response('helloworld.mak', {'a':1})
- self.assertEqual(result.ubody, u'\nHello föö\n')
+ self.assertEqual(result.ubody.replace('\r',''), u'\nHello föö\n')
def test_render_to_response_pkg_spec(self):
from pyramid.renderers import render_to_response
result = render_to_response('pyramid.tests:fixtures/helloworld.mak',
{'a':1})
- self.assertEqual(result.ubody, u'\nHello föö\n')
+ self.assertEqual(result.ubody.replace('\r', ''), u'\nHello föö\n')
def test_render_with_abs_path(self):
from pyramid.renderers import render
- result = render('/helloworld.mak', {'a':1})
+ result = render('/helloworld.mak', {'a':1}).replace('\r','')
self.assertEqual(result, u'\nHello föö\n')
def test_get_renderer(self):
from pyramid.renderers import get_renderer
result = get_renderer('helloworld.mak')
- self.assertEqual(result.implementation().render_unicode(),
+ self.assertEqual(result.implementation().render_unicode().replace('\r',''),
u'\nHello föö\n')
def test_template_not_found(self):