from babel import localedata
-__all__ = ['Locale', 'negotiate', 'parse']
+__all__ = ['UnknownLocaleError', 'Locale', 'negotiate', 'parse']
__docformat__ = 'restructuredtext en'
+class UnknownLocaleError(Exception):
+ """Exception thrown when a locale is requested for which no locale data
+ is available.
+ """
+
+ def __init__(self, identifier):
+ """Create the exception.
+
+ :param identifier: the identifier string of the unsupported locale
+ """
+ Exception.__init__(self, 'unknown locale %r' % identifier)
+ self.identifier = identifier
+
+
class Locale(object):
"""Representation of a specific locale.
>>> locale.number_symbols['decimal']
u'.'
+
+ If a locale is requested for which no locale data is available, an
+ `UnknownLocaleError` is raised:
+
+ >>> Locale.parse('en_DE')
+ Traceback (most recent call last):
+ ...
+ UnknownLocaleError: unknown locale 'en_DE'
:see: `IETF RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_
"""
:param language: the language code
:param territory: the territory (country or region) code
:param variant: the variant code
+ :raise `UnknownLocaleError`: if no locale data is available for the
+ requested locale
"""
self.language = language
self.territory = territory
self.variant = variant
- self._data = localedata.load(str(self))
+ identifier = str(self)
+ try:
+ self._data = localedata.load(identifier)
+ except IOError:
+ raise UnknownLocaleError(identifier)
def parse(cls, identifier, sep='_'):
"""Create a `Locale` instance for the given locale identifier.
:rtype: `Locale`
:raise `ValueError`: if the string does not appear to be a valid locale
identifier
+ :raise `UnknownLocaleError`: if no locale data is available for the
+ requested locale
"""
if type(identifier) is cls:
return identifier
+----------+--------+--------------------------------------------------------+
-Time-zone Support
+Time-Zone Support
-----------------
-Many of the verbose default time formats include the time-zone, but the
-time-zone is not by default available for the Python ``datetime`` and ``time``
-objects. The standard library includes only the abstract ``tzinfo`` class,
-which you need appropriate implementations for to actually use in your
+Many of the verbose time formats include the time-zone, but time-zone
+information is not by default available for the Python ``datetime`` and
+``time`` objects. The standard library includes only the abstract ``tzinfo``
+class, which you need appropriate implementations for to actually use in your
application. Babel includes a ``tzinfo`` implementation for UTC (Universal
-Time). For actual time-zones, it is strongly recommended that you use the
+Time).
+
+For real time-zone support, it is strongly recommended that you use the
third-party package `pytz`_, which includes the definitions of practically all
of the time-zones used on the world, as well as important functions for
reliably converting from UTC to local time, and vice versa::