"""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'
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.
""")
+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.
: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():
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
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.
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.