summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ipalib/errors2.py4
-rw-r--r--ipalib/request.py14
-rw-r--r--tests/test_ipalib/test_request.py68
-rw-r--r--tests/util.py35
4 files changed, 115 insertions, 6 deletions
diff --git a/ipalib/errors2.py b/ipalib/errors2.py
index 7fd4b9c95..4cb84870b 100644
--- a/ipalib/errors2.py
+++ b/ipalib/errors2.py
@@ -38,7 +38,7 @@ to the caller.
"""
from inspect import isclass
-import request
+from request import ugettext, ungettext
class PrivateError(StandardError):
@@ -159,7 +159,7 @@ class PublicError(StandardError):
def __init__(self, message=None, **kw):
self.kw = kw
if message is None:
- message = self.get_format(request._) % kw
+ message = self.get_format() % kw
StandardError.__init__(self, message)
def get_format(self, _):
diff --git a/ipalib/request.py b/ipalib/request.py
index f5400b75c..6ad7ad35f 100644
--- a/ipalib/request.py
+++ b/ipalib/request.py
@@ -32,12 +32,20 @@ from constants import OVERRIDE_ERROR
context = threading.local()
-def _(message):
- if hasattr(context, 'gettext'):
- return context.gettext(message)
+def ugettext(message):
+ if hasattr(context, 'ugettext'):
+ return context.ugettext(message)
return message.decode('UTF-8')
+def ungettext(singular, plural, n):
+ if hasattr(context, 'ungettext'):
+ return context.ungettext(singular, plural, n)
+ if n == 1:
+ return singular.decode('UTF-8')
+ return plural.decode('UTF-8')
+
+
def set_languages(*languages):
if hasattr(context, 'languages'):
raise StandardError(OVERRIDE_ERROR %
diff --git a/tests/test_ipalib/test_request.py b/tests/test_ipalib/test_request.py
index 7096b9274..62afb1403 100644
--- a/tests/test_ipalib/test_request.py
+++ b/tests/test_ipalib/test_request.py
@@ -23,11 +23,77 @@ Test the `ipalib.request` module.
import threading
import locale
-from tests.util import raises, TempDir
+from tests.util import raises, TempDir, DummyUGettext, DummyUNGettext
from ipalib.constants import OVERRIDE_ERROR
from ipalib import request
+def assert_equal(val1, val2):
+ assert type(val1) is type(val2), '%r != %r' % (val1, val2)
+ assert val1 == val2, '%r != %r' % (val1, val2)
+
+
+def test_ugettext():
+ """
+ Test the `ipalib.request.ugettext` function.
+ """
+ f = request.ugettext
+ context = request.context
+ message = 'Hello, world!'
+
+ # Test with no context.ugettext:
+ assert not hasattr(context, 'ugettext')
+ assert_equal(f(message), u'Hello, world!')
+
+ # Test with dummy context.ugettext:
+ assert not hasattr(context, 'ugettext')
+ dummy = DummyUGettext()
+ context.ugettext = dummy
+ assert f(message) is dummy.translation
+ assert dummy.message is message
+
+ # Cleanup
+ del context.ugettext
+ assert not hasattr(context, 'ugettext')
+
+
+def test_ungettext():
+ """
+ Test the `ipalib.request.ungettext` function.
+ """
+ f = request.ungettext
+ context = request.context
+ singular = 'Goose'
+ plural = 'Geese'
+
+ # Test with no context.ungettext:
+ assert not hasattr(context, 'ungettext')
+ assert_equal(f(singular, plural, 1), u'Goose')
+ assert_equal(f(singular, plural, 2), u'Geese')
+
+ # Test singular with dummy context.ungettext
+ assert not hasattr(context, 'ungettext')
+ dummy = DummyUNGettext()
+ context.ungettext = dummy
+ assert f(singular, plural, 1) is dummy.translation_singular
+ assert dummy.singular is singular
+ assert dummy.plural is plural
+ assert dummy.n == 1
+ del context.ungettext
+ assert not hasattr(context, 'ungettext')
+
+ # Test plural with dummy context.ungettext
+ assert not hasattr(context, 'ungettext')
+ dummy = DummyUNGettext()
+ context.ungettext = dummy
+ assert f(singular, plural, 2) is dummy.translation_plural
+ assert dummy.singular is singular
+ assert dummy.plural is plural
+ assert dummy.n == 2
+ del context.ungettext
+ assert not hasattr(context, 'ungettext')
+
+
def test_set_languages():
"""
Test the `ipalib.request.set_languages` function.
diff --git a/tests/util.py b/tests/util.py
index 4a74d2942..66236cbb4 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -277,3 +277,38 @@ class PluginTester(object):
(api, home) = self.finalize(*plugins, **kw)
o = api[namespace][self.plugin.__name__]
return (o, api, home)
+
+
+class DummyUGettext(object):
+ __called = False
+
+ def __init__(self):
+ self.translation = u'The translation'
+
+ def __call__(self, message):
+ assert type(message) is str
+ assert self.__called is False
+ self.__called = True
+ self.message = message
+ return self.translation
+
+
+class DummyUNGettext(object):
+ __called = False
+
+ def __init__(self):
+ self.translation_singular = u'The singular translation'
+ self.translation_plural = u'The plural translation'
+
+ def __call__(self, singular, plural, n):
+ assert type(singular) is str
+ assert type(plural) is str
+ assert type(n) is int
+ assert self.__called is False
+ self.__called = True
+ self.singular = singular
+ self.plural = plural
+ self.n = n
+ if n == 1:
+ return self.translation_singular
+ return self.translation_plural