Module locale
[hide private]
[frames] | no frames]

Module locale

Locale support.

The module provides low-level access to the C lib's locale APIs and adds high level number formatting APIs as well as a locale aliasing engine to complement these.

The aliasing engine includes support for many commonly used locale names and maps them to values suitable for passing to the C lib's setlocale() function. It also includes default encodings for all supported locale names.

Classes [hide private]
Error
Functions [hide private]
 
localeconv()
() -> dict.
 
strcoll(a, b)
string,string -> int.
 
strxfrm(s)
string -> string.
 
_group(s, monetary=False)
 
format(percent, value, grouping=False, monetary=False, *additional)
Returns the locale-aware substitution of a %? specifier (percent).
 
format_string(f, val, grouping=False)
Formats a string in the same way that the % formatting would use, but takes the current locale into account.
 
currency(val, symbol=True, grouping=False, international=False)
Formats val according to the currency settings in the current locale.
 
str(val)
Convert float to integer, taking the locale into account.
 
atof(string, func=<type 'float'>)
Parses a string as a float according to the locale settings.
 
atoi(str)
Converts a string to an integer according to the locale settings.
 
_test()
 
normalize(localename)
Returns a normalized locale code for the given locale name.
 
_parse_localename(localename)
Parses the locale code for localename and returns the result as tuple (language code, encoding).
 
_build_localename(localetuple)
Builds a locale code from the given tuple (language code, encoding).
 
getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'))
Tries to determine the default locale settings and returns them as tuple (language code, encoding).
 
getlocale(category=0)
Returns the current setting for the given locale category as tuple (language code, encoding).
 
setlocale(category, locale=None)
Set the locale for the given category.
 
resetlocale(category=6)
Sets the locale for category to the default setting.
 
getpreferredencoding(do_setlocale=True)
Return the charset that the user is likely using, according to the system configuration.
 
_print_locale()
Test function.
Variables [hide private]
  CHAR_MAX = 127
  LC_ALL = 6
  LC_COLLATE = 3
  LC_CTYPE = 0
  LC_MESSAGES = 5
  LC_MONETARY = 4
  LC_NUMERIC = 1
  LC_TIME = 2
  _percent_re = re.compile(r'%(?:\((?P<key>.*?)\))?(?P<modifiers...
  locale_encoding_alias = {'437': 'C', 'ajec': 'eucJP', 'ascii':...
  locale_alias = {'a3': 'a3_AZ.KOI8-C', 'a3_az': 'a3_AZ.KOI8-C',...
  windows_locale = {4: 'zh_CHS', 1025: 'ar_SA', 1026: 'bg_BG', 1...
  ABDAY_1 = 131072
  ABDAY_2 = 131073
  ABDAY_3 = 131074
  ABDAY_4 = 131075
  ABDAY_5 = 131076
  ABDAY_6 = 131077
  ABDAY_7 = 131078
  ABMON_1 = 131086
  ABMON_10 = 131095
  ABMON_11 = 131096
  ABMON_12 = 131097
  ABMON_2 = 131087
  ABMON_3 = 131088
  ABMON_4 = 131089
  ABMON_5 = 131090
  ABMON_6 = 131091
  ABMON_7 = 131092
  ABMON_8 = 131093
  ABMON_9 = 131094
  ALT_DIGITS = 131119
  AM_STR = 131110
  CODESET = 14
  CRNCYSTR = 262159
  DAY_1 = 131079
  DAY_2 = 131080
  DAY_3 = 131081
  DAY_4 = 131082
  DAY_5 = 131083
  DAY_6 = 131084
  DAY_7 = 131085
  D_FMT = 131113
  D_T_FMT = 131112
  ERA = 131116
  ERA_D_FMT = 131118
  ERA_D_T_FMT = 131120
  ERA_T_FMT = 131121
  MON_1 = 131098
  MON_10 = 131107
  MON_11 = 131108
  MON_12 = 131109
  MON_2 = 131099
  MON_3 = 131100
  MON_4 = 131101
  MON_5 = 131102
  MON_6 = 131103
  MON_7 = 131104
  MON_8 = 131105
  MON_9 = 131106
  NOEXPR = 327681
  PM_STR = 131111
  RADIXCHAR = 65536
  THOUSEP = 65537
  T_FMT = 131114
  T_FMT_AMPM = 131115
  YESEXPR = 327680

Imports: sys, encodings, re, operator, _setlocale, bind_textdomain_codeset, bindtextdomain, dcgettext, dgettext, gettext, nl_langinfo, textdomain


Function Details [hide private]

localeconv()

 

() -> dict. Returns numeric and monetary locale-specific parameters.

strcoll(a, b)

 

string,string -> int. Compares two strings according to the locale.

strxfrm(s)

 

string -> string. Returns a string that behaves for cmp locale-aware.

format(percent, value, grouping=False, monetary=False, *additional)

 

Returns the locale-aware substitution of a %? specifier (percent).

additional is for format strings which contain one or more '*' modifiers.

format_string(f, val, grouping=False)

 

Formats a string in the same way that the % formatting would use, but takes the current locale into account. Grouping is applied if the third parameter is true.

normalize(localename)

 

Returns a normalized locale code for the given locale name.

The returned locale code is formatted for use with setlocale().

If normalization fails, the original name is returned unchanged.

If the given encoding is not known, the function defaults to the default encoding for the locale code just like setlocale() does.

_parse_localename(localename)

 

Parses the locale code for localename and returns the result as tuple (language code, encoding).

The localename is normalized and passed through the locale alias engine. A ValueError is raised in case the locale name cannot be parsed.

The language code corresponds to RFC 1766. code and encoding can be None in case the values cannot be determined or are unknown to this implementation.

_build_localename(localetuple)

 

Builds a locale code from the given tuple (language code, encoding).

No aliasing or normalizing takes place.

getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'))

 
Tries to determine the default locale settings and returns
them as tuple (language code, encoding).

According to POSIX, a program which has not called
setlocale(LC_ALL, "") runs using the portable 'C' locale.
Calling setlocale(LC_ALL, "") lets it use the default locale as
defined by the LANG variable. Since we don't want to interfere
with the current locale setting we thus emulate the behavior
in the way described above.

To maintain compatibility with other platforms, not only the
LANG variable is tested, but a list of variables given as
envvars parameter. The first found to be defined will be
used. envvars defaults to the search path used in GNU gettext;
it must always contain the variable name 'LANG'.

Except for the code 'C', the language code corresponds to RFC
1766.  code and encoding can be None in case the values cannot
be determined.

getlocale(category=0)

 
Returns the current setting for the given locale category as
tuple (language code, encoding).

category may be one of the LC_* value except LC_ALL. It
defaults to LC_CTYPE.

Except for the code 'C', the language code corresponds to RFC
1766.  code and encoding can be None in case the values cannot
be determined.

setlocale(category, locale=None)

 

Set the locale for the given category. The locale can be a string, a locale tuple (language code, encoding), or None.

Locale tuples are converted to strings the locale aliasing engine. Locale strings are passed directly to the C lib.

category may be given as one of the LC_* values.

resetlocale(category=6)

 

Sets the locale for category to the default setting.

The default setting is determined by calling getdefaultlocale(). category defaults to LC_ALL.


Variables Details [hide private]

_percent_re

Value:
re.compile(r'%(?:\((?P<key>.*?)\))?(?P<modifiers>[-#0-9 \+\*\.hlL]*?)[\
eEfFgGdiouxXcrs%]')

locale_encoding_alias

Value:
{'437': 'C',
 'ajec': 'eucJP',
 'ascii': 'ISO8859-1',
 'c': 'C',
 'en': 'ISO8859-1',
 'euc_jp': 'eucJP',
 'euc_kr': 'eucKR',
 'iso2022_jp': 'JIS7',
...

locale_alias

Value:
{'a3': 'a3_AZ.KOI8-C',
 'a3_az': 'a3_AZ.KOI8-C',
 'a3_az.koi8c': 'a3_AZ.KOI8-C',
 'af': 'af_ZA.ISO8859-1',
 'af_za': 'af_ZA.ISO8859-1',
 'af_za.iso88591': 'af_ZA.ISO8859-1',
 'am': 'am_ET.UTF-8',
 'american': 'en_US.ISO8859-1',
...

windows_locale

Value:
{4: 'zh_CHS',
 1025: 'ar_SA',
 1026: 'bg_BG',
 1027: 'ca_ES',
 1028: 'zh_TW',
 1029: 'cs_CZ',
 1030: 'da_DK',
 1031: 'de_DE',
...