Ensure ValidationError gets translated even without msgargs

Closes #1234481.

authorDenis Laxalde <denis.laxalde@logilab.fr>
changeseta293555ee674
branchdefault
phasepublic
hiddenno
parent revision#68b7be49b975 [schema] not foo in bar → foo not in bar
child revision#20ce102e09a5 [py3k] Fix str/unicode in _exceptions.py (related to #2085734)
files modified by this revision
_exceptions.py
test/unittest_schema.py
# HG changeset patch
# User Denis Laxalde <denis.laxalde@logilab.fr>
# Date 1441898982 -7200
# Thu Sep 10 17:29:42 2015 +0200
# Node ID a293555ee67474c1de029c461d066bf100b0da10
# Parent 68b7be49b97517cf193c787a5ef67ad6a7d103b1
Ensure ValidationError gets translated even without msgargs

Closes #1234481.

diff --git a/_exceptions.py b/_exceptions.py
@@ -125,14 +125,16 @@
1          self._translated = True
2          if self.msgargs is not None:
3              if self.i18nvalues:
4                  for key in self.i18nvalues:
5                      self.msgargs[key] = _(self.msgargs[key])
6 -            self.errors = dict(self._translated_errors(_))
7 +        self.errors = dict(self._translated_errors(_))
8 
9      def _translated_errors(self, _):
10          for key, msg in self.errors.items():
11              msg = _(msg)
12              if key is not None:
13                  msg = msg.replace('%(KEY-', '%('+key+'-')
14 -            yield key, msg % self.msgargs
15 +            if self.msgargs:
16 +                msg = msg % self.msgargs
17 +            yield key, msg
18 
diff --git a/test/unittest_schema.py b/test/unittest_schema.py
@@ -499,10 +499,16 @@
19          self.assertEqual(text_type(verr), '1 (None): global message about eid 1')
20          verr.translate(text_type)
21          self.assertEqual(verr.errors,
22                           {None: 'global message about eid 1'})
23 
24 +    def test_validation_error_translate_without_msgargs(self):
25 +        """Check that ValidationError.errors get translated even without msgargs"""
26 +        verr = ValidationError(1, {None: 'hello'})
27 +        verr.translate(list)
28 +        self.assertEqual(verr.errors, {None: list('hello')})
29 +
30      def test_pickle(self):
31          """schema should be pickeable"""
32          import pickle
33          picklefile = mktemp()
34          picklestream = open(picklefile, 'wb')