changeset 39:008de2f257a6 trunk

Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
author cmlenz
date Wed, 06 Jun 2007 11:02:24 +0000
parents 7ae4722af473
children 0739bc8e7210
files babel/core.py babel/dates.py babel/numbers.py babel/util.py
diffstat 4 files changed, 51 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/babel/core.py
+++ b/babel/core.py
@@ -13,9 +13,11 @@
 
 """Core locale representation and locale data access gateway."""
 
+import os
+
 from babel import localedata
 
-__all__ = ['UnknownLocaleError', 'Locale', 'negotiate', 'parse']
+__all__ = ['UnknownLocaleError', 'Locale', 'getdefault', 'negotiate', 'parse']
 __docformat__ = 'restructuredtext en'
 
 
@@ -89,6 +91,23 @@
         except IOError:
             raise UnknownLocaleError(identifier)
 
+    def default(cls, category=None):
+        """Return the system default locale for the specified category.
+        
+        >>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE']:
+        ...     os.environ[name] = ''
+        >>> os.environ['LANG'] = 'fr_FR.UTF-8'
+        >>> Locale.default('LC_MESSAGES')
+        <Locale "fr_FR">
+    
+        :param category: one of the ``LC_XXX`` environment variable names
+        :return: the value of the variable, or any of the fallbacks
+                 (``LANGUAGE``, ``LC_ALL``, ``LC_CTYPE``, and ``LANG``)
+        :rtype: `Locale`
+        """
+        return cls(getdefault(category))
+    default = classmethod(default)
+
     def parse(cls, identifier, sep='_'):
         """Create a `Locale` instance for the given locale identifier.
         
@@ -424,6 +443,28 @@
         """)
 
 
+def getdefault(category=None):
+    """Returns the system default locale for a given category, based on
+    environment variables.
+    
+    >>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE']:
+    ...     os.environ[name] = ''
+    >>> os.environ['LANG'] = 'fr_FR.UTF-8'
+    >>> getdefault('LC_MESSAGES')
+    'fr_FR'
+    
+    :param category: one of the ``LC_XXX`` environment variable names
+    :return: the value of the variable, or any of the fallbacks (``LANGUAGE``,
+             ``LC_ALL``, ``LC_CTYPE``, and ``LANG``)
+
+    :rtype: `str`
+    """
+    varnames = (category, 'LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG')
+    for name in filter(None, varnames):
+        locale = os.getenv(name)
+        if locale:
+            return '_'.join(filter(None, parse(locale)))
+
 def negotiate(preferred, available):
     """Find the best match between available and requested locale strings.
     
@@ -469,6 +510,9 @@
     
     :see: `IETF RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_
     """
+    if '.' in identifier:
+        # this is probably the charset/encoding, which we don't care about
+        identifier = identifier.split('.', 1)[0]
     parts = identifier.split(sep)
     lang, territory, variant = parts[0].lower(), None, None
     if not lang.isalpha():
--- a/babel/dates.py
+++ b/babel/dates.py
@@ -24,14 +24,14 @@
 from datetime import date, datetime, time, timedelta, tzinfo
 import re
 
-from babel.core import Locale
-from babel.util import default_locale, UTC
+from babel.core import getdefault, Locale
+from babel.util import UTC
 
 __all__ = ['format_date', 'format_datetime', 'format_time', 'parse_date',
            'parse_datetime', 'parse_time']
 __docformat__ = 'restructuredtext en'
 
-LC_TIME = default_locale('LC_TIME')
+LC_TIME = getdefault('LC_TIME')
 
 # Aliases for use in scopes where the modules are shadowed by local variables
 date_ = date
--- a/babel/numbers.py
+++ b/babel/numbers.py
@@ -24,15 +24,14 @@
 
 import re
 
-from babel.core import Locale
-from babel.util import default_locale
+from babel.core import getdefault, Locale
 
 __all__ = ['format_number', 'format_decimal', 'format_currency',
            'format_percent', 'format_scientific', 'parse_number',
            'parse_decimal', 'NumberFormatError']
 __docformat__ = 'restructuredtext en'
 
-LC_NUMERIC = default_locale('LC_NUMERIC')
+LC_NUMERIC = getdefault('LC_NUMERIC')
 
 def get_decimal_symbol(locale=LC_NUMERIC):
     """Return the symbol used by the locale to separate decimal fractions.
--- a/babel/util.py
+++ b/babel/util.py
@@ -17,23 +17,9 @@
 import os
 import re
 
-__all__ = ['default_locale', 'extended_glob', 'relpath', 'LazyProxy', 'UTC']
+__all__ = ['extended_glob', 'relpath', 'LazyProxy', 'UTC']
 __docformat__ = 'restructuredtext en'
 
-def default_locale(kind=None):
-    """Returns the default locale for a given category, based on environment
-    variables.
-    
-    :param kind: one of the ``LC_XXX`` environment variable names
-    :return: the value of the variable, or any of the fallbacks (``LC_ALL`` and
-             ``LANG``)
-    :rtype: `str`
-    """
-    for name in filter(None, (kind, 'LC_ALL', 'LANG')):
-        locale = os.getenv(name)
-        if locale is not None:
-            return locale
-
 def extended_glob(pattern, dirname=''):
     """Extended pathname pattern expansion.
     
Copyright (C) 2012-2017 Edgewall Software