# HG changeset patch # User cmlenz # Date 1181127744 0 # Node ID 359ec55de57808534003f8a08497300c8d25ef79 # Parent 496a5c3f9d6d8a4a2d808948ba3446a6af33ace6 Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`. diff --git a/babel/core.py b/babel/core.py --- 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') + + + :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 `_ """ + 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(): diff --git a/babel/dates.py b/babel/dates.py --- 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 diff --git a/babel/numbers.py b/babel/numbers.py --- 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. diff --git a/babel/util.py b/babel/util.py --- 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.