diff options
| author | Christopher Lambacher <chris@kateandchris.net> | 2011-02-12 17:27:59 -0500 |
|---|---|---|
| committer | Christopher Lambacher <chris@kateandchris.net> | 2011-02-12 17:30:15 -0500 |
| commit | 86bbe8bddb1a0e62e7cd811cf2df0d57d72827dc (patch) | |
| tree | 3bf851674726a8530c75d00944e133f5ca25cbcc | |
| parent | b64e90dedd6c469a501ba93f6360ff9ba7a8ef57 (diff) | |
| download | pyramid-86bbe8bddb1a0e62e7cd811cf2df0d57d72827dc.tar.gz pyramid-86bbe8bddb1a0e62e7cd811cf2df0d57d72827dc.tar.bz2 pyramid-86bbe8bddb1a0e62e7cd811cf2df0d57d72827dc.zip | |
Add make_localizer function & docs & tests
| -rw-r--r-- | CONTRIBUTORS.txt | 2 | ||||
| -rw-r--r-- | docs/api/i18n.rst | 2 | ||||
| -rw-r--r-- | docs/narr/i18n.rst | 3 | ||||
| -rw-r--r-- | pyramid/i18n.py | 11 | ||||
| -rw-r--r-- | pyramid/tests/test_i18n.py | 37 |
5 files changed, 51 insertions, 4 deletions
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 2ec0f2f78..4b8281673 100644 --- a/pyramid/i18n.py +++ b/pyramid/i18n.py @@ -145,11 +145,13 @@ def get_locale_name(request): request.locale_name = locale_name return locale_name -def make_localizer(current_locale_name, registry): +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 = {} - tdirs = registry.queryUtility(ITranslationDirectories, default=[]) - for tdir in tdirs: + 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: @@ -187,7 +189,8 @@ def get_localizer(request): if localizer is None: # no localizer utility registered yet - localizer = make_localizer(current_locale_name, registry) + tdirs = registry.queryUtility(ITranslationDirectories, default=[]) + localizer = make_localizer(current_locale_name, tdirs) registry.registerUtility(localizer, ILocalizer, name=current_locale_name) 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() |
