changeset 33:df1e2f0ef627

Raise error on unsupported locales. Closes #5.
author cmlenz
date Mon, 04 Jun 2007 11:29:55 +0000
parents 4527f5a6ed33
children 8b6804eac9e5
files babel/core.py doc/formatting.txt
diffstat 2 files changed, 40 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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 <http://www.ietf.org/rfc/rfc3066.txt>`_
     """
@@ -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
--- 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::
Copyright (C) 2012-2017 Edgewall Software