Michael Merickel
2017-01-17 d009c0bda6723df3124da3b5c10ef6e1f961fcd5
add ``override`` option to ``add_translation_dirs``

fixes #1474
2 files modified
45 ■■■■ changed files
pyramid/config/i18n.py 28 ●●●● patch | view | raw | blame | history
pyramid/tests/test_config/test_i18n.py 17 ●●●●● patch | view | raw | blame | history
pyramid/config/i18n.py
@@ -42,7 +42,7 @@
        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 @@
        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 @@
            # 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 @@
            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)
pyramid/tests/test_config/test_i18n.py
@@ -79,6 +79,23 @@
        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)