Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/pyramid/config/i18n.py : 19%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from pyramid.interfaces import ILocaleNegotiator, ITranslationDirectories
3from pyramid.exceptions import ConfigurationError
4from pyramid.path import AssetResolver
6from pyramid.config.actions import action_method
9class I18NConfiguratorMixin(object):
10 @action_method
11 def set_locale_negotiator(self, negotiator):
12 """
13 Set the :term:`locale negotiator` for this application. The
14 :term:`locale negotiator` is a callable which accepts a
15 :term:`request` object and which returns a :term:`locale
16 name`. The ``negotiator`` argument should be the locale
17 negotiator implementation or a :term:`dotted Python name`
18 which refers to such an implementation.
20 Later calls to this method override earlier calls; there can
21 be only one locale negotiator active at a time within an
22 application. See :ref:`activating_translation` for more
23 information.
25 .. note::
27 Using the ``locale_negotiator`` argument to the
28 :class:`pyramid.config.Configurator` constructor can be used to
29 achieve the same purpose.
30 """
32 def register():
33 self._set_locale_negotiator(negotiator)
35 intr = self.introspectable(
36 'locale negotiator',
37 None,
38 self.object_description(negotiator),
39 'locale negotiator',
40 )
41 intr['negotiator'] = negotiator
42 self.action(ILocaleNegotiator, register, introspectables=(intr,))
44 def _set_locale_negotiator(self, negotiator):
45 locale_negotiator = self.maybe_dotted(negotiator)
46 self.registry.registerUtility(locale_negotiator, ILocaleNegotiator)
48 @action_method
49 def add_translation_dirs(self, *specs, **kw):
50 """ Add one or more :term:`translation directory` paths to the
51 current configuration state. The ``specs`` argument is a
52 sequence that may contain absolute directory paths
53 (e.g. ``/usr/share/locale``) or :term:`asset specification`
54 names naming a directory path (e.g. ``some.package:locale``)
55 or a combination of the two.
57 Example:
59 .. code-block:: python
61 config.add_translation_dirs('/usr/share/locale',
62 'some.package:locale')
64 The translation directories are defined as a list in which
65 translations defined later have precedence over translations defined
66 earlier.
68 By default, consecutive calls to ``add_translation_dirs`` will add
69 directories to the start of the list. This means later calls to
70 ``add_translation_dirs`` will have their translations trumped by
71 earlier calls. If you explicitly need this call to trump an earlier
72 call then you may set ``override`` to ``True``.
74 If multiple specs are provided in a single call to
75 ``add_translation_dirs``, the directories will be inserted in the
76 order they're provided (earlier items are trumped by later items).
78 .. versionchanged:: 1.8
80 The ``override`` parameter was added to allow a later call
81 to ``add_translation_dirs`` to override an earlier call, inserting
82 folders at the beginning of the translation directory list.
84 """
85 introspectables = []
86 override = kw.pop('override', False)
87 if kw:
88 raise TypeError('invalid keyword arguments: %s', sorted(kw.keys()))
90 def register():
91 directories = []
92 resolver = AssetResolver(self.package_name)
94 # defer spec resolution until register to allow for asset
95 # overrides to take place in an earlier config phase
96 for spec in specs:
97 # the trailing slash helps match asset overrides for folders
98 if not spec.endswith('/'):
99 spec += '/'
100 asset = resolver.resolve(spec)
101 directory = asset.abspath()
102 if not asset.isdir():
103 raise ConfigurationError(
104 '"%s" is not a directory' % directory
105 )
106 intr = self.introspectable(
107 'translation directories',
108 directory,
109 spec,
110 'translation directory',
111 )
112 intr['directory'] = directory
113 intr['spec'] = spec
114 introspectables.append(intr)
115 directories.append(directory)
117 tdirs = self.registry.queryUtility(ITranslationDirectories)
118 if tdirs is None:
119 tdirs = []
120 self.registry.registerUtility(tdirs, ITranslationDirectories)
121 if override:
122 tdirs.extend(directories)
123 else:
124 for directory in reversed(directories):
125 tdirs.insert(0, directory)
127 self.action(None, register, introspectables=introspectables)