Changeset 61
- Timestamp:
- 06/08/07 13:44:28 (5 years ago)
- Location:
- trunk/babel
- Files:
-
- 3 edited
-
messages/__init__.py (modified) (1 diff)
-
tests/__init__.py (modified) (1 diff)
-
util.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/babel/messages/__init__.py
r57 r61 14 14 """Support for ``gettext`` message catalogs.""" 15 15 16 import gettext 17 18 __all__ = ['Translations'] 19 __docformat__ = 'restructuredtext en' 20 21 DEFAULT_DOMAIN = 'messages' 22 23 24 class Translations(gettext.GNUTranslations): 25 """An extended translation catalog class.""" 26 27 def __init__(self, fileobj=None): 28 """Initialize the translations catalog. 29 30 :param fileobj: the file-like object the translation should be read 31 from 32 """ 33 gettext.GNUTranslations.__init__(self, fp=fileobj) 34 self.files = [getattr(fileobj, 'name')] 35 36 def load(cls, dirname=None, locales=None, domain=DEFAULT_DOMAIN): 37 """Load translations from the given directory. 38 39 :param dirname: the directory containing the ``MO`` files 40 :param locales: the list of locales in order of preference (items in 41 this list can be either `Locale` objects or locale 42 strings) 43 :param domain: the message domain 44 :return: the loaded catalog, or a ``NullTranslations`` instance if no 45 matching translations were found 46 :rtype: `Translations` 47 """ 48 if not isinstance(locales, (list, tuple)): 49 locales = [locales] 50 locales = [str(locale) for locale in locales] 51 filename = gettext.find(domain, dirname, locales) 52 if not filename: 53 return gettext.NullTranslations() 54 return cls(fileobj=open(filename, 'rb')) 55 load = classmethod(load) 56 57 def merge(self, translations): 58 """Merge the given translations into the catalog. 59 60 Message translations in the specfied catalog override any messages with 61 the same identifier in the existing catalog. 62 63 :param translations: the `Translations` instance with the messages to 64 merge 65 :return: the `Translations` instance (``self``) so that `merge` calls 66 can be easily chained 67 :rtype: `Translations` 68 """ 69 if isinstance(translations, Translations): 70 self._catalog.update(translations._catalog) 71 self.files.extend(translations.files) 72 return self 73 74 def __repr__(self): 75 return "<%s %r>" % (type(self).__name__) 16 from babel.messages.catalog import * -
trunk/babel/tests/__init__.py
r55 r61 21 21 suite.addTest(dates.suite()) 22 22 suite.addTest(localedata.suite()) 23 suite.addTest(messages.suite()) 23 24 suite.addTest(numbers.suite()) 25 suite.addTest(support.suite()) 24 26 suite.addTest(util.suite()) 25 suite.addTest(messages.suite())26 27 return suite 27 28 -
trunk/babel/util.py
r57 r61 18 18 import re 19 19 20 __all__ = ['pathmatch', 'relpath', ' LazyProxy', 'UTC']20 __all__ = ['pathmatch', 'relpath', 'UTC'] 21 21 __docformat__ = 'restructuredtext en' 22 22 … … 117 117 118 118 119 class LazyProxy(object):120 """Class for proxy objects that delegate to a specified function to evaluate121 the actual object.122 123 >>> def greeting(name='world'):124 ... return 'Hello, %s!' % name125 >>> lazy_greeting = LazyProxy(greeting, name='Joe')126 >>> print lazy_greeting127 Hello, Joe!128 >>> u' ' + lazy_greeting129 u' Hello, Joe!'130 >>> u'(%s)' % lazy_greeting131 u'(Hello, Joe!)'132 133 This can be used, for example, to implement lazy translation functions that134 delay the actual translation until the string is actually used. The135 rationale for such behavior is that the locale of the user may not always136 be available. In web applications, you only know the locale when processing137 a request.138 139 The proxy implementation attempts to be as complete as possible, so that140 the lazy objects should mostly work as expected, for example for sorting:141 142 >>> greetings = [143 ... LazyProxy(greeting, 'world'),144 ... LazyProxy(greeting, 'Joe'),145 ... LazyProxy(greeting, 'universe'),146 ... ]147 >>> greetings.sort()148 >>> for greeting in greetings:149 ... print greeting150 Hello, Joe!151 Hello, universe!152 Hello, world!153 """154 __slots__ = ['_func', '_args', '_kwargs', '_value']155 156 def __init__(self, func, *args, **kwargs):157 # Avoid triggering our own __setattr__ implementation158 object.__setattr__(self, '_func', func)159 object.__setattr__(self, '_args', args)160 object.__setattr__(self, '_kwargs', kwargs)161 object.__setattr__(self, '_value', None)162 163 def value(self):164 if self._value is None:165 value = self._func(*self._args, **self._kwargs)166 object.__setattr__(self, '_value', value)167 return self._value168 value = property(value)169 170 def __contains__(self, key):171 return key in self.value172 173 def __nonzero__(self):174 return bool(self.value)175 176 def __dir__(self):177 return dir(self.value)178 179 def __iter__(self):180 return iter(self.value)181 182 def __len__(self):183 return len(self.value)184 185 def __str__(self):186 return str(self.value)187 188 def __unicode__(self):189 return unicode(self.value)190 191 def __add__(self, other):192 return self.value + other193 194 def __radd__(self, other):195 return other + self.value196 197 def __mod__(self, other):198 return self.value % other199 200 def __rmod__(self, other):201 return other % self.value202 203 def __mul__(self, other):204 return self.value * other205 206 def __rmul__(self, other):207 return other * self.value208 209 def __call__(self, *args, **kwargs):210 return self.value(*args, **kwargs)211 212 def __lt__(self, other):213 return self.value < other214 215 def __le__(self, other):216 return self.value <= other217 218 def __eq__(self, other):219 return self.value == other220 221 def __ne__(self, other):222 return self.value != other223 224 def __gt__(self, other):225 return self.value > other226 227 def __ge__(self, other):228 return self.value >= other229 230 def __delattr__(self, name):231 delattr(self.value, name)232 233 def __getattr__(self, name):234 return getattr(self.value, name)235 236 def __setattr__(self, key, value):237 setattr(self.value, name, value)238 239 def __delitem__(self, key):240 del self.value[key]241 242 def __getitem__(self, key):243 return self.value[key]244 245 def __setitem__(self, key, value):246 self.value[name] = value247 248 249 119 try: 250 120 relpath = os.path.relpath
Note: See TracChangeset
for help on using the changeset viewer.
