# HG changeset patch # User cmlenz # Date 1180956595 0 # Node ID b6ff3e4b43e52fd6a9d3bdbd350ea32d0ef4f1a8 # Parent 6b388baae20c9c85742055f734d0a14fc8e5b4e8 Raise error on unsupported locales. Closes #5. diff --git a/babel/core.py b/babel/core.py --- a/babel/core.py +++ b/babel/core.py @@ -15,10 +15,24 @@ 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. @@ -39,6 +53,14 @@ >>> 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 `_ """ @@ -55,11 +77,17 @@ :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. @@ -80,6 +108,8 @@ :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 diff --git a/doc/formatting.txt b/doc/formatting.txt --- a/doc/formatting.txt +++ b/doc/formatting.txt @@ -192,15 +192,17 @@ +----------+--------+--------------------------------------------------------+ -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::