[Zope3-checkins] CVS: Zope3/src/zope/i18n - messageid.py:1.3

Barry Warsaw barry@wooz.org
Wed, 9 Apr 2003 17:22:36 -0400


Update of /cvs-repository/Zope3/src/zope/i18n
In directory cvs.zope.org:/tmp/cvs-serv9277

Modified Files:
	messageid.py 
Log Message:
Slightly cleaner implementation of MessageID.  Logically I think these
should be immutable (but this is not enforced yet since I'm not sure).
However, the domain and default attributes might as well be directly
accessible so get rid of the setDefault() method.

Also add an __new__() so that the domain and default can be specified
in the constructor.  The additional benefit here is that domain and
default are always defined, even if they are set to None (the old way,
you'd have to do a hasattr() first).

Updated docstrings.

Update MessageIDFactory.__call__() to use the new signature for
MessageID.

Whitespace normalization.


=== Zope3/src/zope/i18n/messageid.py 1.2 => 1.3 ===
--- Zope3/src/zope/i18n/messageid.py:1.2	Thu Apr  3 12:25:15 2003
+++ Zope3/src/zope/i18n/messageid.py	Wed Apr  9 17:22:35 2003
@@ -13,34 +13,33 @@
 ##############################################################################
 """Message IDs.
 
-
 $Id$
 """
 
 class MessageID(unicode):
     """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.
-    
+    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.
     """
 
     __slots__ = ('domain', 'default')
 
-    def setDefault(self, default):
-        self.default = default 
+    def __new__(cls, ustr, domain=None, default=None):
+        self = unicode.__new__(cls, ustr)
+        self.domain = domain
+        self.default = default
+        return self
+
 
 class MessageIDFactory:
-    """Factory for creating MessageIDs.
-    """
-    
+    """Factory for creating MessageIDs."""
+
     def __init__(self, domain):
-        self.domain = domain
+        self._domain = domain
 
     def __call__(self, ustr):
-        result = MessageID(ustr)
-        result.domain = self.domain
-        result.default = None
-        return result
-    
+        return MessageID(ustr, domain=self._domain)