summaryrefslogtreecommitdiffstats
path: root/ipalib/text.py
blob: cabca438eb5dc324f1b6fc93c4224f30fd062582 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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=None, localedir=None):
        self.domain = domain
        self.localedir = localedir

    def __mod__(self, kw):
        return self.__unicode__() % kw


class Gettext(LazyText):
    def __init__(self, msg, domain=None, localedir=None):
        self.msg = msg
        super(Gettext, self).__init__(domain, localedir)

    def __unicode__(self):
        return self.msg.decode('utf-8')

    def __repr__(self):
        return '%s(%r)' % (self.__class__.__name__, self.msg)

    def __json__(self):
        return self.__unicode__()


class FixMe(Gettext):
    def __unicode__(self):
        return u'<%s>' % self.msg


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()