summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ipalib/text.py46
-rw-r--r--ipatests/test_ipalib/test_text.py46
-rwxr-xr-xmakeapi4
3 files changed, 94 insertions, 2 deletions
diff --git a/ipalib/text.py b/ipalib/text.py
index c14174843..dd6f3ff20 100644
--- a/ipalib/text.py
+++ b/ipalib/text.py
@@ -136,6 +136,9 @@ class LazyText(object):
This class is not used directly. See the `Gettext` and `NGettext`
subclasses.
+
+ Concatenating LazyText objects with the + operator gives
+ ConcatenatedLazyText objects.
"""
__slots__ = ('domain', 'localedir', 'key', 'args')
@@ -176,6 +179,12 @@ class LazyText(object):
"""
return not self.__eq__(other)
+ def __add__(self, other):
+ return ConcatenatedLazyText(self) + other
+
+ def __radd__(self, other):
+ return other + ConcatenatedLazyText(self)
+
class Gettext(LazyText):
"""
@@ -390,6 +399,43 @@ class NGettext(LazyText):
return ng(self.singular, self.plural, count)
+class ConcatenatedLazyText(object):
+ """Concatenation of multiple strings, or any objects convertible to unicode
+
+ Used to concatenate several LazyTexts together.
+ This allows large strings like help text to be split, so translators
+ do not have to re-translate the whole text when only a small part changes.
+
+ Additional strings may be added to the end with the + or += operators.
+ """
+ def __init__(self, *components):
+ self.components = list(components)
+
+ def __repr__(self):
+ return '%s(%r)' % (self.__class__.__name__, self.components)
+
+ def __unicode__(self):
+ return u''.join(unicode(c) for c in self.components)
+
+ def __json__(self):
+ return unicode(self)
+
+ def __mod__(self, kw):
+ return unicode(self) % kw
+
+ def __add__(self, other):
+ if isinstance(other, ConcatenatedLazyText):
+ return ConcatenatedLazyText(*self.components + other.components)
+ else:
+ return ConcatenatedLazyText(*self.components + [other])
+
+ def __radd__(self, other):
+ if isinstance(other, ConcatenatedLazyText):
+ return ConcatenatedLazyText(*other.components + self.components)
+ else:
+ return ConcatenatedLazyText(*[other] + self.components)
+
+
class GettextFactory(object):
"""
Factory for creating ``_()`` functions.
diff --git a/ipatests/test_ipalib/test_text.py b/ipatests/test_ipalib/test_text.py
index 2a5ff7a36..4d4ce35c8 100644
--- a/ipatests/test_ipalib/test_text.py
+++ b/ipatests/test_ipalib/test_text.py
@@ -332,3 +332,49 @@ class test_NGettextFactory(object):
assert ng.plural is plural
assert ng.domain == 'foo'
assert ng.localedir == 'bar'
+
+
+class test_ConcatenatedText(object):
+
+ klass = text.ConcatenatedLazyText
+
+ def test_init(self):
+ lst = ['a', 'b', 'c', 3]
+ inst = self.klass(*lst)
+ assert inst.components == lst
+ assert unicode(inst) == 'abc3'
+
+ def test_repr(self):
+ lazytext = text.Gettext('foo', 'bar', 'baz')
+ inst = self.klass(lazytext)
+ assert repr(inst) == "ConcatenatedLazyText([%r])" % lazytext
+
+ def test_unicode(self):
+ inst = self.klass('[', text.Gettext('green', 'foo', 'bar'), 1, ']')
+ assert unicode(inst) == u'[green1]'
+
+ def test_mod(self):
+ inst = self.klass('[', text.Gettext('%(color)s', 'foo', 'bar'), ']')
+ assert inst % dict(color='red', stuff='junk') == '[red]'
+
+ def test_add(self):
+ inst = (text.Gettext('pale ', 'foo', 'bar') +
+ text.Gettext('blue', 'foo', 'bar'))
+ assert unicode(inst) == 'pale blue'
+
+ inst = (text.Gettext('bright ', 'foo', 'bar') +
+ text.Gettext('pale ', 'foo', 'bar') +
+ text.Gettext('blue', 'foo', 'bar'))
+ assert unicode(inst) == 'bright pale blue'
+
+ inst = text.Gettext('yellow', 'foo', 'bar') + '!'
+ assert unicode(inst) == 'yellow!'
+
+ inst = '!' + text.Gettext('yellow', 'foo', 'bar')
+ assert unicode(inst) == '!yellow'
+
+ inst = '!' + ('!' + text.Gettext('yellow', 'foo', 'bar'))
+ assert unicode(inst) == '!!yellow'
+
+ inst = (text.Gettext('yellow', 'foo', 'bar') + '!') + '!'
+ assert unicode(inst) == 'yellow!!'
diff --git a/makeapi b/makeapi
index 86907bdbc..d9fb9cffb 100755
--- a/makeapi
+++ b/makeapi
@@ -31,7 +31,7 @@ import operator
from ipalib import api
from ipalib.parameters import Param
from ipalib.output import Output
-from ipalib.text import Gettext, NGettext
+from ipalib.text import Gettext, NGettext, ConcatenatedLazyText
from ipalib.capabilities import capabilities
API_FILE='API.txt'
@@ -120,7 +120,7 @@ def validate_doc():
def is_i18n(obj):
'Helper utility to determine if object has been internationalized'
- return isinstance(obj, (Gettext, NGettext))
+ return isinstance(obj, (Gettext, NGettext, ConcatenatedLazyText))
# The return value
rval = 0