From: Christopher Lenz Date: Mon, 4 Jun 2007 11:29:55 +0000 (+0000) Subject: Raise error on unsupported locales. Closes #5. X-Git-Tag: 1.0~598 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2553c8271db1577a24578ebc8cc0ad74eca80745;p=thirdparty%2Fbabel.git Raise error on unsupported locales. Closes #5. --- diff --git a/babel/core.py b/babel/core.py index bdbef11e..d1ba707c 100644 --- 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 @@ class Locale(object): >>> 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 @@ class Locale(object): :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 @@ class Locale(object): :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 index 61535b06..4800a9a0 100644 --- a/doc/formatting.txt +++ b/doc/formatting.txt @@ -192,15 +192,17 @@ Time Fields +----------+--------+--------------------------------------------------------+ -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::