Edgewall Software
Please note:

The documentation here is generated live, in a way that is neither comprehensive nor 100% correct. We strongly recommend using the HTML documentation included with the source tarballs instead.

babel.core

Core locale representation and locale data access.

UnknownLocaleError

Exception thrown when a locale is requested for which no locale data is available.

Locale

Representation of a specific locale.

>>> locale = Locale('en', 'US')
>>> repr(locale)
'<Locale "en_US">'
>>> locale.display_name
u'English (United States)'

A Locale object can also be instantiated from a raw locale string:

>>> locale = Locale.parse('en-US', sep='-')
>>> repr(locale)
'<Locale "en_US">'

Locale objects provide access to a collection of locale data, such as territory and language names, number and date format patterns, and more:

>>> locale.number_symbols['decimal']
u'.'

If a locale is requested for which no locale data is available, an UnknownLocaleError is raised:

>>> Locale.parse('en_DE')
Traceback (most recent call last):
    ...
UnknownLocaleError: unknown locale 'en_DE'
see:IETF RFC 3066

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

negotiate(cls, preferred, available, sep='_')

Find the best match between available and requested locale strings.

>>> Locale.negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT'])
<Locale "de_DE">
>>> Locale.negotiate(['de_DE', 'en_US'], ['en', 'de'])
<Locale "de">
>>> Locale.negotiate(['de_DE', 'de'], ['en_US'])

You can specify the character used in the locale identifiers to separate the differnet components. This separator is applied to both lists. Also, case is ignored in the comparison:

>>> Locale.negotiate(['de-DE', 'de'], ['en-us', 'de-de'], sep='-')
<Locale "de_DE">
param preferred:
 the list of locale identifers preferred by the user
param available:
 the list of locale identifiers available
return:the Locale object for the best match, or None if no match was found
rtype:Locale

parse(cls, identifier, sep='_')

Create a Locale instance for the given locale identifier.

>>> l = Locale.parse('de-DE', sep='-')
>>> l.display_name
u'Deutsch (Deutschland)'

If the identifier parameter is not a string, but actually a Locale object, that object is returned:

>>> Locale.parse(l)
<Locale "de_DE">
param identifier:
 the locale identifier string
param sep:optional component separator
return:a corresponding Locale instance
rtype:Locale
raise ValueError:
 if the string does not appear to be a valid locale identifier
raise UnknownLocaleError:
 if no locale data is available for the requested locale

get_display_name(self, locale=None)

Return the display name of the locale using the given locale.

The display name will include the language, territory, script, and variant, if those are specified.

>>> Locale('zh', 'CN', script='Hans').get_display_name('en')
u'Chinese (Simplified Han, China)'
param locale:the locale to use
return:the display name

english_name(self)

(Not documented)

languages(self)

(Not documented)

scripts(self)

(Not documented)

territories(self)

(Not documented)

variants(self)

(Not documented)

currencies(self)

(Not documented)

currency_symbols(self)

(Not documented)

number_symbols(self)

(Not documented)

decimal_formats(self)

(Not documented)

currency_formats(self)

(Not documented)

percent_formats(self)

(Not documented)

scientific_formats(self)

(Not documented)

periods(self)

(Not documented)

days(self)

(Not documented)

months(self)

(Not documented)

quarters(self)

(Not documented)

eras(self)

(Not documented)

time_zones(self)

(Not documented)

zone_aliases(self)

(Not documented)

first_week_day(self)

(Not documented)

weekend_start(self)

(Not documented)

weekend_end(self)

(Not documented)

min_week_days(self)

(Not documented)

date_formats(self)

(Not documented)

time_formats(self)

(Not documented)

datetime_formats(self)

(Not documented)

default_locale(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'
>>> default_locale('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

negotiate_locale(preferred, available, sep='_')

Find the best match between available and requested locale strings.

>>> negotiate_locale(['de_DE', 'en_US'], ['de_DE', 'de_AT'])
'de_DE'
>>> negotiate_locale(['de_DE', 'en_US'], ['en', 'de'])
'de'

Case is ignored by the algorithm, the result uses the case of the preferred locale identifier:

>>> negotiate_locale(['de_DE', 'en_US'], ['de_de', 'de_at'])
'de_DE'
param preferred:
 the list of locale strings preferred by the user
param available:
 the list of locale strings available
param sep:character that separates the different parts of the locale strings
return:the locale identifier for the best match, or None if no match was found
rtype:str

parse_locale(identifier, sep='_')

Parse a locale identifier into a tuple of the form:

``(language, territory, script, variant)``
>>> parse_locale('zh_CN')
('zh', 'CN', None, None)
>>> parse_locale('zh_Hans_CN')
('zh', 'CN', 'Hans', None)

The default component separator is "_", but a different separator can be specified using the sep parameter:

>>> parse_locale('zh-CN', sep='-')
('zh', 'CN', None, None)

If the identifier cannot be parsed into a locale, a ValueError exception is raised:

>>> parse_locale('not_a_LOCALE_String')
Traceback (most recent call last):
  ...
ValueError: 'not_a_LOCALE_String' is not a valid locale identifier
param identifier:
 the locale identifier string
param sep:character that separates the different components of the locale identifier
return:the (language, territory, script, variant) tuple
rtype:tuple
raise ValueError:
 if the string does not appear to be a valid locale identifier
see:IETF RFC 4646


See ApiDocs/0.8, Documentation

Last modified 18 months ago Last modified on Dec 10, 2015 6:25:47 AM