From: Christopher Lenz Date: Wed, 6 Jun 2007 11:02:24 +0000 (+0000) Subject: Move function for determining the system default locale to `babel.core`, and make... X-Git-Tag: 1.0~590 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=34005c1b9e0cd56e8758bdc4d27ad642d5e11131;p=thirdparty%2Fbabel.git 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 index 31f6d245..ec43a95d 100644 --- 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 @@ class Locale(object): 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 @@ class Locale(object): """) +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 @@ def parse(identifier, sep='_'): :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 index 42a9f24a..338ff428 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -24,14 +24,14 @@ following environment variables, in that order: 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 index 9c23e241..d102ceb2 100644 --- a/babel/numbers.py +++ b/babel/numbers.py @@ -24,15 +24,14 @@ following environment variables, in that order: 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 index 943a8d39..62070e1e 100644 --- a/babel/util.py +++ b/babel/util.py @@ -17,23 +17,9 @@ from datetime import timedelta, tzinfo 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.