diff options
| author | Michael Merickel <michael@merickel.org> | 2017-01-17 01:41:51 -0600 |
|---|---|---|
| committer | Michael Merickel <michael@merickel.org> | 2017-01-17 02:05:05 -0600 |
| commit | d009c0bda6723df3124da3b5c10ef6e1f961fcd5 (patch) | |
| tree | 52cca06cb5c5034d4ae1c81fc064c1ee20ade036 | |
| parent | c1374524655ac9da42c6f45281f5e11b086f5973 (diff) | |
| download | pyramid-d009c0bda6723df3124da3b5c10ef6e1f961fcd5.tar.gz pyramid-d009c0bda6723df3124da3b5c10ef6e1f961fcd5.tar.bz2 pyramid-d009c0bda6723df3124da3b5c10ef6e1f961fcd5.zip | |
add ``override`` option to ``add_translation_dirs``
fixes #1474
| -rw-r--r-- | pyramid/config/i18n.py | 28 | ||||
| -rw-r--r-- | pyramid/tests/test_config/test_i18n.py | 17 |
2 files changed, 37 insertions, 8 deletions
diff --git a/pyramid/config/i18n.py b/pyramid/config/i18n.py index 96fd30a51..9c5b7e682 100644 --- a/pyramid/config/i18n.py +++ b/pyramid/config/i18n.py @@ -42,7 +42,7 @@ class I18NConfiguratorMixin(object): self.registry.registerUtility(locale_negotiator, ILocaleNegotiator) @action_method - def add_translation_dirs(self, *specs): + def add_translation_dirs(self, *specs, **kw): """ Add one or more :term:`translation directory` paths to the current configuration state. The ``specs`` argument is a sequence that may contain absolute directory paths @@ -61,18 +61,27 @@ class I18NConfiguratorMixin(object): translations defined later have precedence over translations defined earlier. + By default, consecutive calls to ``add_translation_dirs`` will add + directories to the start of the list. This means later calls to + ``add_translation_dirs`` will have their translations trumped by + earlier calls. If you explicitly need this call to trump an earlier + call then you may set ``override`` to ``True``. + If multiple specs are provided in a single call to ``add_translation_dirs``, the directories will be inserted in the order they're provided (earlier items are trumped by later items). - .. warning:: + .. versionchanged:: 1.8 - Consecutive calls to ``add_translation_dirs`` will sort the - directories such that the later calls will add folders with - lower precedence than earlier calls. + The ``override`` parameter was added to allow a later call + to ``add_translation_dirs`` to override an earlier call, inserting + folders at the beginning of the translation directory list. """ introspectables = [] + override = kw.pop('override', False) + if kw: + raise TypeError('invalid keyword arguments: %s', sorted(kw.keys())) def register(): directories = [] @@ -80,7 +89,7 @@ class I18NConfiguratorMixin(object): # defer spec resolution until register to allow for asset # overrides to take place in an earlier config phase - for spec in specs[::-1]: # reversed + for spec in specs: # the trailing slash helps match asset overrides for folders if not spec.endswith('/'): spec += '/' @@ -100,8 +109,11 @@ class I18NConfiguratorMixin(object): if tdirs is None: tdirs = [] self.registry.registerUtility(tdirs, ITranslationDirectories) - for directory in directories: - tdirs.insert(0, directory) + if override: + tdirs.extend(directories) + else: + for directory in reversed(directories): + tdirs.insert(0, directory) self.action(None, register, introspectables=introspectables) diff --git a/pyramid/tests/test_config/test_i18n.py b/pyramid/tests/test_config/test_i18n.py index adfb6191c..c10ab6bdb 100644 --- a/pyramid/tests/test_config/test_i18n.py +++ b/pyramid/tests/test_config/test_i18n.py @@ -79,6 +79,23 @@ class TestI18NConfiguratorMixin(unittest.TestCase): self.assertEqual(config.registry.getUtility(ITranslationDirectories), [locale3, locale, locale2]) + def test_add_translation_dirs_override_multiple_specs_multiple_calls(self): + from pyramid.interfaces import ITranslationDirectories + config = self._makeOne(autocommit=True) + config.add_translation_dirs('pyramid.tests.pkgs.localeapp:locale', + 'pyramid.tests.pkgs.localeapp:locale2') + config.add_translation_dirs('pyramid.tests.pkgs.localeapp:locale3', + override=True) + self.assertEqual(config.registry.getUtility(ITranslationDirectories), + [locale, locale2, locale3]) + + def test_add_translation_dirs_invalid_kwargs(self): + from pyramid.interfaces import ITranslationDirectories + config = self._makeOne(autocommit=True) + with self.assertRaises(TypeError): + config.add_translation_dirs('pyramid.tests.pkgs.localeapp:locale', + foo=1) + def test_add_translation_dirs_abspath(self): from pyramid.interfaces import ITranslationDirectories config = self._makeOne(autocommit=True) |
