Skip to content

Commit

Permalink
add override option to add_translation_dirs
Browse files Browse the repository at this point in the history
fixes #1474
  • Loading branch information
mmerickel committed Jan 17, 2017
1 parent c137452 commit d009c0b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
28 changes: 20 additions & 8 deletions pyramid/config/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _set_locale_negotiator(self, negotiator):
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
Expand All @@ -61,26 +61,35 @@ def add_translation_dirs(self, *specs):
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 = []
resolver = AssetResolver(self.package_name)

# 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 += '/'
Expand All @@ -100,8 +109,11 @@ def register():
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)

17 changes: 17 additions & 0 deletions pyramid/tests/test_config/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ def test_add_translation_dirs_multiple_specs_multiple_calls(self):
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)
Expand Down

0 comments on commit d009c0b

Please sign in to comment.