]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Force normalization of locale ID.
authorKevin Deldycke <kdeldycke@scaleway.com>
Wed, 2 Mar 2016 16:04:13 +0000 (17:04 +0100)
committerKevin Deldycke <kdeldycke@scaleway.com>
Wed, 2 Mar 2016 16:04:13 +0000 (17:04 +0100)
babel/localedata.py

index 437f49fae39f5a7be3563efeb1c0ddc2da590047..a20a8813af876685653b07e52e0f31803ebf941a 100644 (file)
@@ -15,6 +15,7 @@
 import os
 import threading
 from collections import MutableMapping
+from itertools import chain
 
 from babel._compat import pickle
 
@@ -24,15 +25,22 @@ _cache_lock = threading.RLock()
 _dirname = os.path.join(os.path.dirname(__file__), 'locale-data')
 
 
+def normalize_locale(name):
+    """Return a normalized locale ID or `None` if the ID is not recognized."""
+    name = name.strip().lower()
+    for locale_id in chain.from_iterable([_cache, locale_identifiers()]):
+        if name == locale_id.lower():
+            return locale_id
+
+
 def exists(name):
-    """Check whether locale data is available for the given locale.  Ther
-    return value is `True` if it exists, `False` otherwise.
+    """Check whether locale data is available for the given locale.
+
+    Returns `True` if it exists, `False` otherwise.
 
     :param name: the locale identifier string
     """
-    if name in _cache:
-        return True
-    return os.path.exists(os.path.join(_dirname, '%s.dat' % name))
+    return True if normalize_locale(name) else False
 
 
 def locale_identifiers():