]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale...
authorChristopher Lenz <cmlenz@gmail.com>
Wed, 6 Jun 2007 12:12:44 +0000 (12:12 +0000)
committerChristopher Lenz <cmlenz@gmail.com>
Wed, 6 Jun 2007 12:12:44 +0000 (12:12 +0000)
babel/core.py
babel/localedata.py

index ec43a95d059110858431fc57aa421779de9753e3..b9aad27d71905a40f49de1e154b98d44424dcdb0 100644 (file)
@@ -85,10 +85,10 @@ class Locale(object):
         self.language = language
         self.territory = territory
         self.variant = variant
+        self.__data = None
+
         identifier = str(self)
-        try:
-            self._data = localedata.load(identifier)
-        except IOError:
+        if not localedata.exists(identifier):
             raise UnknownLocaleError(identifier)
 
     def default(cls, category=None):
@@ -142,13 +142,20 @@ class Locale(object):
         return '_'.join(filter(None, [self.language, self.territory,
                                       self.variant]))
 
+    def _data(self):
+        if self.__data is None:
+            self.__data = localedata.load(str(self))
+        return self.__data
+    _data = property(_data)
+
     def display_name(self):
         retval = self.languages.get(self.language)
         if self.territory:
             variant = ''
             if self.variant:
                 variant = ', %s' % self.variants.get(self.variant)
-            retval += ' (%s%s)' % (self.territories.get(self.territory), variant)
+            retval += ' (%s%s)' % (self.territories.get(self.territory),
+                                   variant)
         return retval
     display_name = property(display_name, doc="""\
         The localized display name of the locale.
@@ -157,6 +164,8 @@ class Locale(object):
         u'English'
         >>> Locale('en', 'US').display_name
         u'English (United States)'
+        >>> Locale('sv').display_name
+        u'svenska'
         
         :type: `unicode`
         """)
index 95e4d58f36485f969f24ce53f62559e3cbe95abe..cf3fa04b3fb487f75524831cd74df013fae64f9b 100644 (file)
@@ -30,6 +30,18 @@ __docformat__ = 'restructuredtext en'
 
 _cache = {}
 _cache_lock = threading.RLock()
+_dirname = os.path.join(os.path.dirname(__file__), 'localedata')
+
+def exists(name):
+    """Check whether locale data is available for the given locale.
+    
+    :param name: the locale identifier string
+    :return: `True` if the locale data exists, `False` otherwise
+    :rtype: `bool`
+    """
+    if name in _cache:
+        return True
+    return os.path.exists(os.path.join(_dirname, '%s.dat' % name))
 
 def load(name):
     """Load the locale data for the given locale.
@@ -70,8 +82,7 @@ def load(name):
                 else:
                     parent = '_'.join(parts[:-1])
                 data = load(parent).copy()
-            filename = os.path.join(os.path.dirname(__file__),
-                                    'localedata/%s.dat' % name)
+            filename = os.path.join(_dirname, '%s.dat' % name)
             fileobj = open(filename, 'rb')
             try:
                 if name != 'root':