summaryrefslogtreecommitdiffstats
path: root/ipalib/text.py
diff options
context:
space:
mode:
authorPetr Viktorin <pviktori@redhat.com>2013-04-11 12:27:25 +0200
committerPetr Viktorin <pviktori@redhat.com>2013-11-21 10:34:25 +0100
commit8f57f25e82f64e338b4cec1fb58fa27f3d432f8f (patch)
tree38613ffd8788ff28e378e4205f8af718ac4a19e3 /ipalib/text.py
parent35c3a5f161b1ac3ce9d55371e2abde52455cf403 (diff)
downloadfreeipa-8f57f25e82f64e338b4cec1fb58fa27f3d432f8f.tar.gz
freeipa-8f57f25e82f64e338b4cec1fb58fa27f3d432f8f.tar.xz
freeipa-8f57f25e82f64e338b4cec1fb58fa27f3d432f8f.zip
Add ConcatenatedLazyText object
This object will allow splitting large translatable strings into more pieces, so translators don't have to re-translate the entire text when a small part changes. https://fedorahosted.org/freeipa/ticket/3587
Diffstat (limited to 'ipalib/text.py')
-rw-r--r--ipalib/text.py46
1 files changed, 46 insertions, 0 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.