diff options
| -rw-r--r-- | CHANGES.txt | 6 | ||||
| -rw-r--r-- | pyramid/i18n.py | 7 | ||||
| -rw-r--r-- | pyramid/tests/test_i18n.py | 13 |
3 files changed, 25 insertions, 1 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 9dd1af2c5..0254ac2b0 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -42,6 +42,12 @@ Documentation Features -------- +- Add support for language fallbacks: when trying to translate for a + specific territory (such as ``en_GB``) fall back to translations + for the language (ie ``en``). This brings the translation behaviour in line + with GNU gettext and fixes partially translated texts when using C + extensions. + - New authentication policy: ``pyramid.authentication.SessionAuthenticationPolicy``, which uses a session to store credentials. diff --git a/pyramid/i18n.py b/pyramid/i18n.py index 4b8281673..7bc096bb1 100644 --- a/pyramid/i18n.py +++ b/pyramid/i18n.py @@ -151,11 +151,16 @@ def make_localizer(current_locale_name, translation_directories): translations found in the list of translation directories.""" translations = Translations() translations._catalog = {} + + locales_to_try = [current_locale_name] + if '_' in current_locale_name: + locales_to_try.append(current_locale_name.split('_')[0]) + 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: + if locale_name not in locales_to_try: continue messages_dir = os.path.join(locale_dir, 'LC_MESSAGES') if not os.path.isdir(os.path.realpath(messages_dir)): diff --git a/pyramid/tests/test_i18n.py b/pyramid/tests/test_i18n.py index 588f0a7cc..97117a8cd 100644 --- a/pyramid/tests/test_i18n.py +++ b/pyramid/tests/test_i18n.py @@ -200,6 +200,19 @@ class Test_make_localizer(unittest.TestCase): self.assertEqual(result.translate('Approve', 'deformsite'), 'Approve') + def test_territory_fallback(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_DE' + result = self._callFUT(locale_name, localedirs) + self.assertEqual(result.__class__, Localizer) + self.assertEqual(result.translate('Approve', 'deformsite'), + 'Genehmigen') + + class Test_get_localizer(unittest.TestCase): def setUp(self): cleanUp() |
