File: MessageID.py

package info (click to toggle)
zope-pts 1.2-rc1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 356 kB
  • ctags: 311
  • sloc: python: 1,756; makefile: 89; sh: 62
file content (167 lines) | stat: -rw-r--r-- 5,328 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, LUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Message IDs.

Message Id factor based on the i18n/messageid file of Zope 3.

Adapted for the Placeless Translation Service by Christian Heimes

$Id: MessageID.py,v 1.6 2004/05/04 21:56:03 dreamcatcher Exp $
"""
from types import BuiltinFunctionType, UnicodeType, StringType

from Globals import InitializeClass
from AccessControl import ClassSecurityInfo

from Products.PlacelessTranslationService import translate, utranslate

# MessageID requires the get_request patch to work
from PatchStringIO import applyRequestPatch
applyRequestPatch()

try:
    True
except NameError:
    True=1
    False=0


class MessageIDBase:
    """Message ID.

    This is a string used as a message ID.  It has a domain attribute that is
    its source domain, and a default attribute that is its default text to
    display when there is no translation.  domain may be None meaning there is
    no translation domain.  default may also be None, in which case the
    message id itself implicitly serves as the default text.

    MessageID objects also have a mapping attribute which must be set after
    construction of the object.  This is used when translating and
    substituting variables.
    """
    security = ClassSecurityInfo()

    def __init__(self, ustr, domain=None, default=None, default_encoding=None):
        self.ustr = ustr
        self.domain  = domain
        if default is None:
            self.default = self.ustr
        else:
            self.default = default
        self.default_encoding = default_encoding
        self.mapping = {}

    def translate(self):
        pass

    def __str__(self):
        pass

    def __call__(self):
        return self.translate()

    def __getattr__XXX(self, func):
        """try to emulate class MessageID(unicode)
        """
        attr = getattr(self(), func, None)
        if attr and type(attr) is BuiltinFunctionType:
            return attr
        raise AttributeError("'MessageId' has no attribute '%s'" % func)

InitializeClass(MessageIDBase)


class MessageID(MessageIDBase):
    """non unicode MessageID
    """
    security = ClassSecurityInfo()

    security.declarePublic('translate')
    def translate(self):
        """translate the message id
        """
        # get the context (aka request) in the translate method itself
        return translate(domain=self.domain,
                            msgid=self.ustr,
                            mapping=self.mapping,
                            default=self.default)

    def __str__(self):
        return self.translate()

InitializeClass(MessageID)


class MessageIDUnicode(MessageIDBase):
    """unicode MessageID
    """
    security = ClassSecurityInfo()

    def __init__(self, ustr, domain=None, default=None, default_encoding=None):
        MessageIDBase.__init__(self, ustr, domain, default, default_encoding)
        if default_encoding and type(ustr) is not UnicodeType:
            ustr = unicode(ustr, default_encoding)
        else:
            ustr = unicode(ustr)
        self.ustr = ustr
        if default is None:
            self.default = ustr
        else:
            if default_encoding and type(ustr) is not UnicodeType:
                self.default = unicode(default, default_encoding)
            else:
                self.ustr = unicode(default)

    security.declarePublic('translate')
    def translate(self):
        """translate the message id
        """
        # get the context (aka request) in the translate method itself
        return utranslate(domain=self.domain,
                            msgid=self.ustr,
                            mapping=self.mapping,
                            default=self.default)

    def __str__(self):
        return self.translate()

InitializeClass(MessageIDUnicode)


class MessageIDFactory:
    """Factory for creating MessageIDs
    """
    security = ClassSecurityInfo()

    def __init__(self, domain, as_unicode=False, default_encoding=None):
        self._domain = domain
        self._as_unicode = as_unicode
        self._default_encoding = default_encoding

    def __call__(self, ustr, default=None):
        """used for _()

        ustr - the message id
        default - the default string if the message id isn't the default
        """
        if self._as_unicode:
            return MessageIDUnicode(ustr, domain=self._domain,
                                    default=default,
                                    default_encoding=self._default_encoding)
        else:
            return MessageID(ustr, domain=self._domain,
                             default=default,
                             default_encoding=self._default_encoding)

InitializeClass(MessageIDFactory)