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
|
##############################################################################
#
# Copyright (c) 2004 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (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, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""I18n Messages
"""
__docformat__ = "reStructuredText"
class Message(unicode):
"""Message (Python implementation)
This is a string used as a message. 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.
These are the doc tests from message.txt. Note that we have to create the
message manually since MessageFactory would return the C implementation.
>>> from zope.i18nmessageid.message import Message
>>> robot = Message(u"robot-message", 'futurama', u"${name} is a robot.")
>>> robot == u'robot-message'
True
>>> isinstance(robot, unicode)
True
>>> robot.default == u'${name} is a robot.'
True
>>> robot.mapping
>>> robot.domain = "planetexpress"
Traceback (most recent call last):
...
TypeError: readonly attribute
>>> robot.default = u"${name} is not a robot."
Traceback (most recent call last):
...
TypeError: readonly attribute
>>> robot.mapping = {u'name': u'Bender'}
Traceback (most recent call last):
...
TypeError: readonly attribute
>>> new_robot = Message(robot, mapping={u'name': u'Bender'})
>>> new_robot == u'robot-message'
True
>>> new_robot.domain == 'futurama'
True
>>> new_robot.default == u'${name} is a robot.'
True
>>> new_robot.mapping == {u'name': u'Bender'}
True
>>> callable, args = new_robot.__reduce__()
>>> callable is Message
True
>>> args == (u'robot-message',
... 'futurama',
... u'${name} is a robot.',
... {u'name': u'Bender'})
True
>>> fembot = Message(u'fembot')
>>> callable, args = fembot.__reduce__()
>>> callable is Message
True
>>> args == (u'fembot', None, None, None)
True
Check if pickling and unpickling works
>>> from pickle import dumps, loads
>>> pystate = dumps(new_robot)
>>> pickle_bot = loads(pystate)
>>> (pickle_bot,
... pickle_bot.domain,
... pickle_bot.default,
... pickle_bot.mapping) == (u'robot-message',
... 'futurama',
... u'${name} is a robot.',
... {u'name': u'Bender'})
True
>>> pickle_bot.__reduce__()[0] is Message
True
"""
__slots__ = ('domain', 'default', 'mapping', '_readonly')
def __new__(cls, ustr, domain=None, default=None, mapping=None):
self = unicode.__new__(cls, ustr)
if isinstance(ustr, self.__class__):
domain = ustr.domain and ustr.domain[:] or domain
default = ustr.default and ustr.default[:] or default
mapping = ustr.mapping and ustr.mapping.copy() or mapping
ustr = unicode(ustr)
self.domain = domain
if default is None:
# MessageID does: self.default = ustr
self.default = default
else:
self.default = unicode(default)
self.mapping = mapping
self._readonly = True
return self
def __setattr__(self, key, value):
"""Message is immutable
It cannot be changed once the message id is created.
"""
if getattr(self, '_readonly', False):
raise TypeError('readonly attribute')
else:
return unicode.__setattr__(self, key, value)
def __reduce__(self):
return self.__class__, self.__getstate__()
def __getstate__(self):
return unicode(self), self.domain, self.default, self.mapping
# save a copy for the unit tests
pyMessage = Message
try:
from _zope_i18nmessageid_message import Message
except ImportError: # pragma: no cover
pass
class MessageFactory(object):
"""Factory for creating i18n messages."""
def __init__(self, domain):
self._domain = domain
def __call__(self, ustr, default=None, mapping=None):
return Message(ustr, self._domain, default, mapping)
|