summaryrefslogtreecommitdiffstats
path: root/ipalib/text.py
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2009-12-09 09:09:53 -0700
committerJason Gerard DeRose <jderose@redhat.com>2009-12-10 08:29:15 -0700
commitb6e4972e7f6aa08e0392a2cf441b60ab0e7d88b7 (patch)
tree7e5329a51af169ce34a7d275a1bbd63c1e31c026 /ipalib/text.py
parentd08b8858ddc3bf6265f6ea8acae6661b9fff5112 (diff)
downloadfreeipa-b6e4972e7f6aa08e0392a2cf441b60ab0e7d88b7.tar.gz
freeipa-b6e4972e7f6aa08e0392a2cf441b60ab0e7d88b7.tar.xz
freeipa-b6e4972e7f6aa08e0392a2cf441b60ab0e7d88b7.zip
Take 2: Extensible return values and validation; steps toward a single output_for_cli(); enable more webUI stuff
Diffstat (limited to 'ipalib/text.py')
-rw-r--r--ipalib/text.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/ipalib/text.py b/ipalib/text.py
new file mode 100644
index 000000000..0c8684025
--- /dev/null
+++ b/ipalib/text.py
@@ -0,0 +1,79 @@
+# Authors:
+# Jason Gerard DeRose <jderose@redhat.com>
+#
+# Copyright (C) 2009 Red Hat
+# see file 'COPYING' for use and warranty contextrmation
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; version 2 only
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+"""
+Thread-local lazy gettext service.
+
+TODO: This aren't hooked up into gettext yet, they currently just provide
+placeholders for the rest of the code.
+"""
+
+
+class LazyText(object):
+ def __init__(self, domain, localedir):
+ self.domain = domain
+ self.localedir = localedir
+
+
+class Gettext(LazyText):
+ def __init__(self, msg, domain, localedir):
+ self.msg = msg
+ super(Gettext, self).__init__(domain, localedir)
+
+ def __unicode__(self):
+ return self.msg.decode('utf-8')
+
+ def __mod__(self, value):
+ return self.__unicode__() % value
+
+
+class NGettext(LazyText):
+ def __init__(self, singular, plural, domain, localedir):
+ self.singular = singular
+ self.plural = plural
+ super(NGettext, self).__init__(domain, localedir)
+
+ def __mod__(self, kw):
+ count = kw['count']
+ return self(count) % kw
+
+ def __call__(self, count):
+ if count == 1:
+ return self.singular.decode('utf-8')
+ return self.plural.decode('utf-8')
+
+
+class gettext_factory(object):
+ def __init__(self, domain='ipa', localedir=None):
+ self.domain = domain
+ self.localedir = localedir
+
+ def __call__(self, msg):
+ return Gettext(msg, self.domain, self.localedir)
+
+
+class ngettext_factory(gettext_factory):
+ def __call__(self, singular, plural, count=0):
+ return NGettext(singular, plural, self.domain, self.localedir)
+
+
+# Process wide factories:
+gettext = gettext_factory()
+_ = gettext
+ngettext = ngettext_factory()