2000-06-07 17:11:40 +08:00
|
|
|
""" Locale support.
|
2000-02-04 23:39:30 +08:00
|
|
|
|
2000-06-07 17:11:40 +08:00
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
1997-11-20 03:01:43 +08:00
|
|
|
|
2001-02-09 18:48:30 +08:00
|
|
|
import sys
|
1997-11-20 03:01:43 +08:00
|
|
|
|
2000-07-10 01:12:58 +08:00
|
|
|
# Try importing the _locale module.
|
|
|
|
#
|
|
|
|
# If this fails, fall back on a basic 'C' locale emulation.
|
2000-06-07 17:11:40 +08:00
|
|
|
|
2001-01-24 18:13:46 +08:00
|
|
|
# Yuck: LC_MESSAGES is non-standard: can't tell whether it exists before
|
|
|
|
# trying the import. So __all__ is also fiddled at the end of the file.
|
2001-01-24 14:27:27 +08:00
|
|
|
__all__ = ["setlocale","Error","localeconv","strcoll","strxfrm",
|
|
|
|
"format","str","atof","atoi","LC_CTYPE","LC_COLLATE",
|
2001-01-24 18:13:46 +08:00
|
|
|
"LC_TIME","LC_MONETARY","LC_NUMERIC", "LC_ALL","CHAR_MAX"]
|
2001-01-24 14:27:27 +08:00
|
|
|
|
2000-06-09 01:49:41 +08:00
|
|
|
try:
|
2000-07-10 01:12:58 +08:00
|
|
|
|
2000-06-09 01:49:41 +08:00
|
|
|
from _locale import *
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
|
2000-07-10 01:12:58 +08:00
|
|
|
# Locale emulation
|
|
|
|
|
2000-06-09 01:49:41 +08:00
|
|
|
CHAR_MAX = 127
|
|
|
|
LC_ALL = 6
|
|
|
|
LC_COLLATE = 3
|
|
|
|
LC_CTYPE = 0
|
|
|
|
LC_MESSAGES = 5
|
|
|
|
LC_MONETARY = 4
|
|
|
|
LC_NUMERIC = 1
|
|
|
|
LC_TIME = 2
|
|
|
|
Error = ValueError
|
|
|
|
|
|
|
|
def localeconv():
|
2000-07-10 01:12:58 +08:00
|
|
|
""" localeconv() -> dict.
|
2000-06-09 01:49:41 +08:00
|
|
|
Returns numeric and monetary locale-specific parameters.
|
|
|
|
"""
|
|
|
|
# 'C' locale default values
|
|
|
|
return {'grouping': [127],
|
|
|
|
'currency_symbol': '',
|
|
|
|
'n_sign_posn': 127,
|
2000-07-10 01:12:58 +08:00
|
|
|
'p_cs_precedes': 127,
|
|
|
|
'n_cs_precedes': 127,
|
|
|
|
'mon_grouping': [],
|
2000-06-09 01:49:41 +08:00
|
|
|
'n_sep_by_space': 127,
|
|
|
|
'decimal_point': '.',
|
|
|
|
'negative_sign': '',
|
|
|
|
'positive_sign': '',
|
2000-07-10 01:12:58 +08:00
|
|
|
'p_sep_by_space': 127,
|
2000-06-09 01:49:41 +08:00
|
|
|
'int_curr_symbol': '',
|
2000-07-10 01:12:58 +08:00
|
|
|
'p_sign_posn': 127,
|
2000-06-09 01:49:41 +08:00
|
|
|
'thousands_sep': '',
|
2000-07-10 01:12:58 +08:00
|
|
|
'mon_thousands_sep': '',
|
|
|
|
'frac_digits': 127,
|
2000-06-09 01:49:41 +08:00
|
|
|
'mon_decimal_point': '',
|
|
|
|
'int_frac_digits': 127}
|
2000-07-10 01:12:58 +08:00
|
|
|
|
2000-06-09 01:49:41 +08:00
|
|
|
def setlocale(category, value=None):
|
2000-07-10 01:12:58 +08:00
|
|
|
""" setlocale(integer,string=None) -> string.
|
2000-06-09 01:49:41 +08:00
|
|
|
Activates/queries locale processing.
|
|
|
|
"""
|
2001-03-24 01:00:07 +08:00
|
|
|
if value is not None and value != 'C':
|
2000-07-10 01:12:58 +08:00
|
|
|
raise Error, '_locale emulation only supports "C" locale'
|
2000-06-09 01:49:41 +08:00
|
|
|
return 'C'
|
|
|
|
|
|
|
|
def strcoll(a,b):
|
2000-07-10 01:12:58 +08:00
|
|
|
""" strcoll(string,string) -> int.
|
2000-06-09 01:49:41 +08:00
|
|
|
Compares two strings according to the locale.
|
|
|
|
"""
|
|
|
|
return cmp(a,b)
|
|
|
|
|
|
|
|
def strxfrm(s):
|
2000-07-10 01:12:58 +08:00
|
|
|
""" strxfrm(string) -> string.
|
2000-06-09 01:49:41 +08:00
|
|
|
Returns a string that behaves for cmp locale-aware.
|
|
|
|
"""
|
|
|
|
return s
|
2000-06-07 17:11:40 +08:00
|
|
|
|
|
|
|
### Number formatting APIs
|
|
|
|
|
|
|
|
# Author: Martin von Loewis
|
|
|
|
|
1997-11-20 03:01:43 +08:00
|
|
|
#perform the grouping from right to left
|
|
|
|
def _group(s):
|
|
|
|
conv=localeconv()
|
|
|
|
grouping=conv['grouping']
|
2001-04-17 00:04:10 +08:00
|
|
|
if not grouping:return (s, 0)
|
1997-11-20 03:01:43 +08:00
|
|
|
result=""
|
2001-04-13 16:09:50 +08:00
|
|
|
seps = 0
|
|
|
|
spaces = ""
|
|
|
|
if s[-1] == ' ':
|
|
|
|
sp = s.find(' ')
|
|
|
|
spaces = s[sp:]
|
|
|
|
s = s[:sp]
|
1997-11-20 03:01:43 +08:00
|
|
|
while s and grouping:
|
2000-07-10 01:12:58 +08:00
|
|
|
# if grouping is -1, we are done
|
1998-03-27 05:13:24 +08:00
|
|
|
if grouping[0]==CHAR_MAX:
|
|
|
|
break
|
|
|
|
# 0: re-use last group ad infinitum
|
|
|
|
elif grouping[0]!=0:
|
|
|
|
#process last group
|
|
|
|
group=grouping[0]
|
|
|
|
grouping=grouping[1:]
|
|
|
|
if result:
|
|
|
|
result=s[-group:]+conv['thousands_sep']+result
|
2001-04-13 16:09:50 +08:00
|
|
|
seps += 1
|
1998-03-27 05:13:24 +08:00
|
|
|
else:
|
|
|
|
result=s[-group:]
|
|
|
|
s=s[:-group]
|
2001-04-13 16:09:50 +08:00
|
|
|
if s and s[-1] not in "0123456789":
|
|
|
|
# the leading string is only spaces and signs
|
|
|
|
return s+result+spaces,seps
|
2000-06-07 17:11:40 +08:00
|
|
|
if not result:
|
2001-04-13 16:09:50 +08:00
|
|
|
return s+spaces,seps
|
2000-06-07 17:11:40 +08:00
|
|
|
if s:
|
1998-03-27 05:13:24 +08:00
|
|
|
result=s+conv['thousands_sep']+result
|
2001-04-13 16:09:50 +08:00
|
|
|
seps += 1
|
|
|
|
return result+spaces,seps
|
1997-11-20 03:01:43 +08:00
|
|
|
|
|
|
|
def format(f,val,grouping=0):
|
|
|
|
"""Formats a value in the same way that the % formatting would use,
|
2000-07-10 01:12:58 +08:00
|
|
|
but takes the current locale into account.
|
1997-11-20 03:01:43 +08:00
|
|
|
Grouping is applied if the third parameter is true."""
|
2001-04-13 16:09:50 +08:00
|
|
|
result = f % val
|
2001-01-22 02:52:33 +08:00
|
|
|
fields = result.split(".")
|
2001-04-13 16:09:50 +08:00
|
|
|
seps = 0
|
1997-11-20 03:01:43 +08:00
|
|
|
if grouping:
|
2001-04-13 16:09:50 +08:00
|
|
|
fields[0],seps=_group(fields[0])
|
1997-11-20 03:01:43 +08:00
|
|
|
if len(fields)==2:
|
2001-04-13 16:09:50 +08:00
|
|
|
result = fields[0]+localeconv()['decimal_point']+fields[1]
|
1997-11-20 03:01:43 +08:00
|
|
|
elif len(fields)==1:
|
2001-04-13 16:09:50 +08:00
|
|
|
result = fields[0]
|
1997-11-20 03:01:43 +08:00
|
|
|
else:
|
2000-07-10 01:12:58 +08:00
|
|
|
raise Error, "Too many decimal points in result string"
|
|
|
|
|
2001-04-13 16:09:50 +08:00
|
|
|
while seps:
|
|
|
|
# If the number was formatted for a specific width, then it
|
|
|
|
# might have been filled with spaces to the left or right. If
|
|
|
|
# so, kill as much spaces as there where separators.
|
|
|
|
# Leading zeroes as fillers are not yet dealt with, as it is
|
|
|
|
# not clear how they should interact with grouping.
|
|
|
|
sp = result.find(" ")
|
|
|
|
if sp==-1:break
|
|
|
|
result = result[:sp]+result[sp+1:]
|
|
|
|
seps -= 1
|
|
|
|
|
|
|
|
return result
|
2001-01-22 02:52:33 +08:00
|
|
|
|
1997-11-20 03:01:43 +08:00
|
|
|
def str(val):
|
|
|
|
"""Convert float to integer, taking the locale into account."""
|
|
|
|
return format("%.12g",val)
|
|
|
|
|
2001-02-09 18:48:30 +08:00
|
|
|
def atof(str,func=float):
|
1997-11-20 03:01:43 +08:00
|
|
|
"Parses a string as a float according to the locale settings."
|
|
|
|
#First, get rid of the grouping
|
2000-06-07 17:11:40 +08:00
|
|
|
ts = localeconv()['thousands_sep']
|
|
|
|
if ts:
|
2001-02-09 18:48:30 +08:00
|
|
|
s=str.split(ts)
|
|
|
|
str="".join(s)
|
1997-11-20 03:01:43 +08:00
|
|
|
#next, replace the decimal point with a dot
|
2000-06-07 17:11:40 +08:00
|
|
|
dd = localeconv()['decimal_point']
|
|
|
|
if dd:
|
2001-02-09 18:48:30 +08:00
|
|
|
s=str.split(dd)
|
|
|
|
str='.'.join(s)
|
1997-11-20 03:01:43 +08:00
|
|
|
#finally, parse the string
|
|
|
|
return func(str)
|
|
|
|
|
|
|
|
def atoi(str):
|
|
|
|
"Converts a string to an integer according to the locale settings."
|
2001-02-09 18:48:30 +08:00
|
|
|
return atof(str, int)
|
1997-11-20 03:01:43 +08:00
|
|
|
|
2000-06-07 17:11:40 +08:00
|
|
|
def _test():
|
2000-07-10 01:12:58 +08:00
|
|
|
setlocale(LC_ALL, "")
|
1997-11-20 03:01:43 +08:00
|
|
|
#do grouping
|
2000-07-10 01:12:58 +08:00
|
|
|
s1=format("%d", 123456789,1)
|
|
|
|
print s1, "is", atoi(s1)
|
1997-11-20 03:01:43 +08:00
|
|
|
#standard formatting
|
|
|
|
s1=str(3.14)
|
2000-07-10 01:12:58 +08:00
|
|
|
print s1, "is", atof(s1)
|
2000-06-07 17:11:40 +08:00
|
|
|
|
|
|
|
### Locale name aliasing engine
|
|
|
|
|
|
|
|
# Author: Marc-Andre Lemburg, mal@lemburg.com
|
2002-10-20 04:19:10 +08:00
|
|
|
# Various tweaks by Fredrik Lundh <fredrik@pythonware.com>
|
2000-07-10 01:12:58 +08:00
|
|
|
|
|
|
|
# store away the low-level version of setlocale (it's
|
|
|
|
# overridden below)
|
|
|
|
_setlocale = setlocale
|
2000-06-07 17:11:40 +08:00
|
|
|
|
|
|
|
def 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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
# Normalize the locale name and extract the encoding
|
2001-02-09 18:48:30 +08:00
|
|
|
fullname = localename.lower()
|
2000-06-07 17:11:40 +08:00
|
|
|
if ':' in fullname:
|
|
|
|
# ':' is sometimes used as encoding delimiter.
|
2001-02-09 18:48:30 +08:00
|
|
|
fullname = fullname.replace(':', '.')
|
2000-06-07 17:11:40 +08:00
|
|
|
if '.' in fullname:
|
2001-02-09 18:48:30 +08:00
|
|
|
langname, encoding = fullname.split('.')[:2]
|
2000-06-07 17:11:40 +08:00
|
|
|
fullname = langname + '.' + encoding
|
|
|
|
else:
|
|
|
|
langname = fullname
|
|
|
|
encoding = ''
|
|
|
|
|
|
|
|
# First lookup: fullname (possibly with encoding)
|
|
|
|
code = locale_alias.get(fullname, None)
|
|
|
|
if code is not None:
|
|
|
|
return code
|
|
|
|
|
|
|
|
# Second try: langname (without encoding)
|
|
|
|
code = locale_alias.get(langname, None)
|
|
|
|
if code is not None:
|
|
|
|
if '.' in code:
|
2001-02-09 18:48:30 +08:00
|
|
|
langname, defenc = code.split('.')
|
2000-06-07 17:11:40 +08:00
|
|
|
else:
|
|
|
|
langname = code
|
|
|
|
defenc = ''
|
|
|
|
if encoding:
|
|
|
|
encoding = encoding_alias.get(encoding, encoding)
|
|
|
|
else:
|
|
|
|
encoding = defenc
|
|
|
|
if encoding:
|
|
|
|
return langname + '.' + encoding
|
|
|
|
else:
|
|
|
|
return langname
|
|
|
|
|
|
|
|
else:
|
|
|
|
return localename
|
|
|
|
|
|
|
|
def _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
|
2000-06-28 22:48:01 +08:00
|
|
|
unknown to this implementation.
|
2000-06-07 17:11:40 +08:00
|
|
|
|
|
|
|
"""
|
|
|
|
code = normalize(localename)
|
|
|
|
if '.' in code:
|
2001-02-09 18:48:30 +08:00
|
|
|
return code.split('.')[:2]
|
2000-06-07 17:11:40 +08:00
|
|
|
elif code == 'C':
|
|
|
|
return None, None
|
2001-08-13 22:50:44 +08:00
|
|
|
raise ValueError, 'unknown locale: %s' % localename
|
2000-06-07 17:11:40 +08:00
|
|
|
|
|
|
|
def _build_localename(localetuple):
|
|
|
|
|
|
|
|
""" Builds a locale code from the given tuple (language code,
|
|
|
|
encoding).
|
|
|
|
|
|
|
|
No aliasing or normalizing takes place.
|
|
|
|
|
|
|
|
"""
|
|
|
|
language, encoding = localetuple
|
|
|
|
if language is None:
|
|
|
|
language = 'C'
|
|
|
|
if encoding is None:
|
|
|
|
return language
|
|
|
|
else:
|
|
|
|
return language + '.' + encoding
|
2000-07-10 01:12:58 +08:00
|
|
|
|
|
|
|
def getdefaultlocale(envvars=('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG')):
|
2000-06-07 17:11:40 +08:00
|
|
|
|
|
|
|
""" Tries to determine the default locale settings and returns
|
|
|
|
them as tuple (language code, encoding).
|
|
|
|
|
|
|
|
According to POSIX, a program which has not called
|
2000-07-10 01:12:58 +08:00
|
|
|
setlocale(LC_ALL, "") runs using the portable 'C' locale.
|
|
|
|
Calling setlocale(LC_ALL, "") lets it use the default locale as
|
2000-06-07 17:11:40 +08:00
|
|
|
defined by the LANG variable. Since we don't want to interfere
|
2000-07-16 20:04:32 +08:00
|
|
|
with the current locale setting we thus emulate the behavior
|
2000-06-07 17:11:40 +08:00
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
2000-07-10 07:16:10 +08:00
|
|
|
|
2000-07-10 01:12:58 +08:00
|
|
|
try:
|
|
|
|
# check if it's supported by the _locale module
|
|
|
|
import _locale
|
|
|
|
code, encoding = _locale._getdefaultlocale()
|
2000-07-10 07:16:10 +08:00
|
|
|
except (ImportError, AttributeError):
|
|
|
|
pass
|
|
|
|
else:
|
2000-07-11 03:32:19 +08:00
|
|
|
# make sure the code/encoding values are valid
|
2000-07-10 01:12:58 +08:00
|
|
|
if sys.platform == "win32" and code and code[:2] == "0x":
|
|
|
|
# map windows language identifier to language name
|
|
|
|
code = windows_locale.get(int(code, 0))
|
2000-07-11 03:32:19 +08:00
|
|
|
# ...add other platform-specific processing here, if
|
|
|
|
# necessary...
|
2000-07-10 01:12:58 +08:00
|
|
|
return code, encoding
|
2000-07-10 07:16:10 +08:00
|
|
|
|
2000-07-10 01:12:58 +08:00
|
|
|
# fall back on POSIX behaviour
|
2000-06-07 17:11:40 +08:00
|
|
|
import os
|
|
|
|
lookup = os.environ.get
|
|
|
|
for variable in envvars:
|
|
|
|
localename = lookup(variable,None)
|
|
|
|
if localename is not None:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
localename = 'C'
|
|
|
|
return _parse_localename(localename)
|
|
|
|
|
2000-07-10 01:12:58 +08:00
|
|
|
|
|
|
|
def getlocale(category=LC_CTYPE):
|
2000-06-07 17:11:40 +08:00
|
|
|
|
|
|
|
""" 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.
|
|
|
|
|
|
|
|
"""
|
2000-07-10 01:12:58 +08:00
|
|
|
localename = _setlocale(category)
|
2000-06-07 17:11:40 +08:00
|
|
|
if category == LC_ALL and ';' in localename:
|
2000-07-10 01:12:58 +08:00
|
|
|
raise TypeError, 'category LC_ALL is not supported'
|
2000-06-07 17:11:40 +08:00
|
|
|
return _parse_localename(localename)
|
|
|
|
|
2000-07-10 01:12:58 +08:00
|
|
|
def setlocale(category, locale=None):
|
2000-06-07 17:11:40 +08:00
|
|
|
|
2000-07-10 01:12:58 +08:00
|
|
|
""" Set the locale for the given category. The locale can be
|
|
|
|
a string, a locale tuple (language code, encoding), or None.
|
2000-06-07 17:11:40 +08:00
|
|
|
|
2000-07-10 01:12:58 +08:00
|
|
|
Locale tuples are converted to strings the locale aliasing
|
|
|
|
engine. Locale strings are passed directly to the C lib.
|
2000-06-07 17:11:40 +08:00
|
|
|
|
2000-07-10 01:12:58 +08:00
|
|
|
category may be given as one of the LC_* values.
|
2000-06-07 17:11:40 +08:00
|
|
|
|
|
|
|
"""
|
2000-07-10 01:12:58 +08:00
|
|
|
if locale and type(locale) is not type(""):
|
|
|
|
# convert to string
|
|
|
|
locale = normalize(_build_localename(locale))
|
|
|
|
return _setlocale(category, locale)
|
2000-06-07 17:11:40 +08:00
|
|
|
|
2000-07-10 01:12:58 +08:00
|
|
|
def resetlocale(category=LC_ALL):
|
2000-06-07 17:11:40 +08:00
|
|
|
|
|
|
|
""" Sets the locale for category to the default setting.
|
|
|
|
|
|
|
|
The default setting is determined by calling
|
2000-07-10 01:12:58 +08:00
|
|
|
getdefaultlocale(). category defaults to LC_ALL.
|
|
|
|
|
2000-06-07 17:11:40 +08:00
|
|
|
"""
|
2000-07-10 01:12:58 +08:00
|
|
|
_setlocale(category, _build_localename(getdefaultlocale()))
|
2000-06-07 17:11:40 +08:00
|
|
|
|
|
|
|
### Database
|
|
|
|
#
|
|
|
|
# The following data was extracted from the locale.alias file which
|
|
|
|
# comes with X11 and then hand edited removing the explicit encoding
|
|
|
|
# definitions and adding some more aliases. The file is usually
|
|
|
|
# available as /usr/lib/X11/locale/locale.alias.
|
2000-07-10 01:12:58 +08:00
|
|
|
#
|
2000-06-07 17:11:40 +08:00
|
|
|
|
|
|
|
#
|
|
|
|
# The encoding_alias table maps lowercase encoding alias names to C
|
|
|
|
# locale encoding names (case-sensitive).
|
|
|
|
#
|
|
|
|
encoding_alias = {
|
2000-07-10 01:12:58 +08:00
|
|
|
'437': 'C',
|
|
|
|
'c': 'C',
|
|
|
|
'iso8859': 'ISO8859-1',
|
|
|
|
'8859': 'ISO8859-1',
|
|
|
|
'88591': 'ISO8859-1',
|
|
|
|
'ascii': 'ISO8859-1',
|
|
|
|
'en': 'ISO8859-1',
|
|
|
|
'iso88591': 'ISO8859-1',
|
|
|
|
'iso_8859-1': 'ISO8859-1',
|
|
|
|
'885915': 'ISO8859-15',
|
|
|
|
'iso885915': 'ISO8859-15',
|
|
|
|
'iso_8859-15': 'ISO8859-15',
|
|
|
|
'iso8859-2': 'ISO8859-2',
|
|
|
|
'iso88592': 'ISO8859-2',
|
|
|
|
'iso_8859-2': 'ISO8859-2',
|
|
|
|
'iso88595': 'ISO8859-5',
|
|
|
|
'iso88596': 'ISO8859-6',
|
|
|
|
'iso88597': 'ISO8859-7',
|
|
|
|
'iso88598': 'ISO8859-8',
|
|
|
|
'iso88599': 'ISO8859-9',
|
|
|
|
'iso-2022-jp': 'JIS7',
|
|
|
|
'jis': 'JIS7',
|
|
|
|
'jis7': 'JIS7',
|
|
|
|
'sjis': 'SJIS',
|
|
|
|
'tis620': 'TACTIS',
|
|
|
|
'ajec': 'eucJP',
|
|
|
|
'eucjp': 'eucJP',
|
|
|
|
'ujis': 'eucJP',
|
|
|
|
'utf-8': 'utf',
|
|
|
|
'utf8': 'utf',
|
|
|
|
'utf8@ucs4': 'utf',
|
2000-06-07 17:11:40 +08:00
|
|
|
}
|
|
|
|
|
2000-07-10 01:12:58 +08:00
|
|
|
#
|
2000-06-07 17:11:40 +08:00
|
|
|
# The locale_alias table maps lowercase alias names to C locale names
|
|
|
|
# (case-sensitive). Encodings are always separated from the locale
|
|
|
|
# name using a dot ('.'); they should only be given in case the
|
|
|
|
# language name is needed to interpret the given encoding alias
|
|
|
|
# correctly (CJK codes often have this need).
|
|
|
|
#
|
|
|
|
locale_alias = {
|
|
|
|
'american': 'en_US.ISO8859-1',
|
|
|
|
'ar': 'ar_AA.ISO8859-6',
|
|
|
|
'ar_aa': 'ar_AA.ISO8859-6',
|
|
|
|
'ar_sa': 'ar_SA.ISO8859-6',
|
|
|
|
'arabic': 'ar_AA.ISO8859-6',
|
|
|
|
'bg': 'bg_BG.ISO8859-5',
|
|
|
|
'bg_bg': 'bg_BG.ISO8859-5',
|
|
|
|
'bulgarian': 'bg_BG.ISO8859-5',
|
|
|
|
'c-french': 'fr_CA.ISO8859-1',
|
|
|
|
'c': 'C',
|
|
|
|
'c_c': 'C',
|
|
|
|
'cextend': 'en_US.ISO8859-1',
|
|
|
|
'chinese-s': 'zh_CN.eucCN',
|
|
|
|
'chinese-t': 'zh_TW.eucTW',
|
|
|
|
'croatian': 'hr_HR.ISO8859-2',
|
|
|
|
'cs': 'cs_CZ.ISO8859-2',
|
|
|
|
'cs_cs': 'cs_CZ.ISO8859-2',
|
|
|
|
'cs_cz': 'cs_CZ.ISO8859-2',
|
|
|
|
'cz': 'cz_CZ.ISO8859-2',
|
|
|
|
'cz_cz': 'cz_CZ.ISO8859-2',
|
|
|
|
'czech': 'cs_CS.ISO8859-2',
|
|
|
|
'da': 'da_DK.ISO8859-1',
|
|
|
|
'da_dk': 'da_DK.ISO8859-1',
|
|
|
|
'danish': 'da_DK.ISO8859-1',
|
|
|
|
'de': 'de_DE.ISO8859-1',
|
|
|
|
'de_at': 'de_AT.ISO8859-1',
|
|
|
|
'de_ch': 'de_CH.ISO8859-1',
|
|
|
|
'de_de': 'de_DE.ISO8859-1',
|
|
|
|
'dutch': 'nl_BE.ISO8859-1',
|
|
|
|
'ee': 'ee_EE.ISO8859-4',
|
|
|
|
'el': 'el_GR.ISO8859-7',
|
|
|
|
'el_gr': 'el_GR.ISO8859-7',
|
|
|
|
'en': 'en_US.ISO8859-1',
|
|
|
|
'en_au': 'en_AU.ISO8859-1',
|
|
|
|
'en_ca': 'en_CA.ISO8859-1',
|
|
|
|
'en_gb': 'en_GB.ISO8859-1',
|
|
|
|
'en_ie': 'en_IE.ISO8859-1',
|
|
|
|
'en_nz': 'en_NZ.ISO8859-1',
|
|
|
|
'en_uk': 'en_GB.ISO8859-1',
|
|
|
|
'en_us': 'en_US.ISO8859-1',
|
|
|
|
'eng_gb': 'en_GB.ISO8859-1',
|
|
|
|
'english': 'en_EN.ISO8859-1',
|
|
|
|
'english_uk': 'en_GB.ISO8859-1',
|
|
|
|
'english_united-states': 'en_US.ISO8859-1',
|
|
|
|
'english_us': 'en_US.ISO8859-1',
|
|
|
|
'es': 'es_ES.ISO8859-1',
|
|
|
|
'es_ar': 'es_AR.ISO8859-1',
|
|
|
|
'es_bo': 'es_BO.ISO8859-1',
|
|
|
|
'es_cl': 'es_CL.ISO8859-1',
|
|
|
|
'es_co': 'es_CO.ISO8859-1',
|
|
|
|
'es_cr': 'es_CR.ISO8859-1',
|
|
|
|
'es_ec': 'es_EC.ISO8859-1',
|
|
|
|
'es_es': 'es_ES.ISO8859-1',
|
|
|
|
'es_gt': 'es_GT.ISO8859-1',
|
|
|
|
'es_mx': 'es_MX.ISO8859-1',
|
|
|
|
'es_ni': 'es_NI.ISO8859-1',
|
|
|
|
'es_pa': 'es_PA.ISO8859-1',
|
|
|
|
'es_pe': 'es_PE.ISO8859-1',
|
|
|
|
'es_py': 'es_PY.ISO8859-1',
|
|
|
|
'es_sv': 'es_SV.ISO8859-1',
|
|
|
|
'es_uy': 'es_UY.ISO8859-1',
|
|
|
|
'es_ve': 'es_VE.ISO8859-1',
|
|
|
|
'et': 'et_EE.ISO8859-4',
|
|
|
|
'et_ee': 'et_EE.ISO8859-4',
|
|
|
|
'fi': 'fi_FI.ISO8859-1',
|
|
|
|
'fi_fi': 'fi_FI.ISO8859-1',
|
|
|
|
'finnish': 'fi_FI.ISO8859-1',
|
|
|
|
'fr': 'fr_FR.ISO8859-1',
|
|
|
|
'fr_be': 'fr_BE.ISO8859-1',
|
|
|
|
'fr_ca': 'fr_CA.ISO8859-1',
|
|
|
|
'fr_ch': 'fr_CH.ISO8859-1',
|
|
|
|
'fr_fr': 'fr_FR.ISO8859-1',
|
|
|
|
'fre_fr': 'fr_FR.ISO8859-1',
|
|
|
|
'french': 'fr_FR.ISO8859-1',
|
|
|
|
'french_france': 'fr_FR.ISO8859-1',
|
|
|
|
'ger_de': 'de_DE.ISO8859-1',
|
|
|
|
'german': 'de_DE.ISO8859-1',
|
|
|
|
'german_germany': 'de_DE.ISO8859-1',
|
|
|
|
'greek': 'el_GR.ISO8859-7',
|
|
|
|
'hebrew': 'iw_IL.ISO8859-8',
|
|
|
|
'hr': 'hr_HR.ISO8859-2',
|
|
|
|
'hr_hr': 'hr_HR.ISO8859-2',
|
|
|
|
'hu': 'hu_HU.ISO8859-2',
|
|
|
|
'hu_hu': 'hu_HU.ISO8859-2',
|
|
|
|
'hungarian': 'hu_HU.ISO8859-2',
|
|
|
|
'icelandic': 'is_IS.ISO8859-1',
|
|
|
|
'id': 'id_ID.ISO8859-1',
|
|
|
|
'id_id': 'id_ID.ISO8859-1',
|
|
|
|
'is': 'is_IS.ISO8859-1',
|
|
|
|
'is_is': 'is_IS.ISO8859-1',
|
|
|
|
'iso-8859-1': 'en_US.ISO8859-1',
|
|
|
|
'iso-8859-15': 'en_US.ISO8859-15',
|
|
|
|
'iso8859-1': 'en_US.ISO8859-1',
|
|
|
|
'iso8859-15': 'en_US.ISO8859-15',
|
|
|
|
'iso_8859_1': 'en_US.ISO8859-1',
|
|
|
|
'iso_8859_15': 'en_US.ISO8859-15',
|
|
|
|
'it': 'it_IT.ISO8859-1',
|
|
|
|
'it_ch': 'it_CH.ISO8859-1',
|
|
|
|
'it_it': 'it_IT.ISO8859-1',
|
|
|
|
'italian': 'it_IT.ISO8859-1',
|
|
|
|
'iw': 'iw_IL.ISO8859-8',
|
|
|
|
'iw_il': 'iw_IL.ISO8859-8',
|
|
|
|
'ja': 'ja_JP.eucJP',
|
|
|
|
'ja.jis': 'ja_JP.JIS7',
|
|
|
|
'ja.sjis': 'ja_JP.SJIS',
|
|
|
|
'ja_jp': 'ja_JP.eucJP',
|
|
|
|
'ja_jp.ajec': 'ja_JP.eucJP',
|
|
|
|
'ja_jp.euc': 'ja_JP.eucJP',
|
|
|
|
'ja_jp.eucjp': 'ja_JP.eucJP',
|
|
|
|
'ja_jp.iso-2022-jp': 'ja_JP.JIS7',
|
|
|
|
'ja_jp.jis': 'ja_JP.JIS7',
|
|
|
|
'ja_jp.jis7': 'ja_JP.JIS7',
|
|
|
|
'ja_jp.mscode': 'ja_JP.SJIS',
|
|
|
|
'ja_jp.sjis': 'ja_JP.SJIS',
|
|
|
|
'ja_jp.ujis': 'ja_JP.eucJP',
|
|
|
|
'japan': 'ja_JP.eucJP',
|
|
|
|
'japanese': 'ja_JP.SJIS',
|
|
|
|
'japanese-euc': 'ja_JP.eucJP',
|
|
|
|
'japanese.euc': 'ja_JP.eucJP',
|
|
|
|
'jp_jp': 'ja_JP.eucJP',
|
|
|
|
'ko': 'ko_KR.eucKR',
|
|
|
|
'ko_kr': 'ko_KR.eucKR',
|
|
|
|
'ko_kr.euc': 'ko_KR.eucKR',
|
|
|
|
'korean': 'ko_KR.eucKR',
|
|
|
|
'lt': 'lt_LT.ISO8859-4',
|
|
|
|
'lv': 'lv_LV.ISO8859-4',
|
|
|
|
'mk': 'mk_MK.ISO8859-5',
|
|
|
|
'mk_mk': 'mk_MK.ISO8859-5',
|
|
|
|
'nl': 'nl_NL.ISO8859-1',
|
|
|
|
'nl_be': 'nl_BE.ISO8859-1',
|
|
|
|
'nl_nl': 'nl_NL.ISO8859-1',
|
|
|
|
'no': 'no_NO.ISO8859-1',
|
|
|
|
'no_no': 'no_NO.ISO8859-1',
|
|
|
|
'norwegian': 'no_NO.ISO8859-1',
|
|
|
|
'pl': 'pl_PL.ISO8859-2',
|
|
|
|
'pl_pl': 'pl_PL.ISO8859-2',
|
|
|
|
'polish': 'pl_PL.ISO8859-2',
|
|
|
|
'portuguese': 'pt_PT.ISO8859-1',
|
|
|
|
'portuguese_brazil': 'pt_BR.ISO8859-1',
|
|
|
|
'posix': 'C',
|
|
|
|
'posix-utf2': 'C',
|
|
|
|
'pt': 'pt_PT.ISO8859-1',
|
|
|
|
'pt_br': 'pt_BR.ISO8859-1',
|
|
|
|
'pt_pt': 'pt_PT.ISO8859-1',
|
|
|
|
'ro': 'ro_RO.ISO8859-2',
|
|
|
|
'ro_ro': 'ro_RO.ISO8859-2',
|
|
|
|
'ru': 'ru_RU.ISO8859-5',
|
|
|
|
'ru_ru': 'ru_RU.ISO8859-5',
|
|
|
|
'rumanian': 'ro_RO.ISO8859-2',
|
|
|
|
'russian': 'ru_RU.ISO8859-5',
|
|
|
|
'serbocroatian': 'sh_YU.ISO8859-2',
|
|
|
|
'sh': 'sh_YU.ISO8859-2',
|
|
|
|
'sh_hr': 'sh_HR.ISO8859-2',
|
|
|
|
'sh_sp': 'sh_YU.ISO8859-2',
|
|
|
|
'sh_yu': 'sh_YU.ISO8859-2',
|
|
|
|
'sk': 'sk_SK.ISO8859-2',
|
|
|
|
'sk_sk': 'sk_SK.ISO8859-2',
|
|
|
|
'sl': 'sl_CS.ISO8859-2',
|
|
|
|
'sl_cs': 'sl_CS.ISO8859-2',
|
|
|
|
'sl_si': 'sl_SI.ISO8859-2',
|
|
|
|
'slovak': 'sk_SK.ISO8859-2',
|
|
|
|
'slovene': 'sl_CS.ISO8859-2',
|
|
|
|
'sp': 'sp_YU.ISO8859-5',
|
|
|
|
'sp_yu': 'sp_YU.ISO8859-5',
|
|
|
|
'spanish': 'es_ES.ISO8859-1',
|
|
|
|
'spanish_spain': 'es_ES.ISO8859-1',
|
|
|
|
'sr_sp': 'sr_SP.ISO8859-2',
|
|
|
|
'sv': 'sv_SE.ISO8859-1',
|
|
|
|
'sv_se': 'sv_SE.ISO8859-1',
|
|
|
|
'swedish': 'sv_SE.ISO8859-1',
|
|
|
|
'th_th': 'th_TH.TACTIS',
|
|
|
|
'tr': 'tr_TR.ISO8859-9',
|
|
|
|
'tr_tr': 'tr_TR.ISO8859-9',
|
|
|
|
'turkish': 'tr_TR.ISO8859-9',
|
|
|
|
'univ': 'en_US.utf',
|
|
|
|
'universal': 'en_US.utf',
|
|
|
|
'zh': 'zh_CN.eucCN',
|
|
|
|
'zh_cn': 'zh_CN.eucCN',
|
|
|
|
'zh_cn.big5': 'zh_TW.eucTW',
|
|
|
|
'zh_cn.euc': 'zh_CN.eucCN',
|
|
|
|
'zh_tw': 'zh_TW.eucTW',
|
|
|
|
'zh_tw.euc': 'zh_TW.eucTW',
|
|
|
|
}
|
|
|
|
|
2000-07-10 01:12:58 +08:00
|
|
|
#
|
|
|
|
# this maps windows language identifiers (as used on Windows 95 and
|
|
|
|
# earlier) to locale strings.
|
|
|
|
#
|
2002-10-20 04:19:10 +08:00
|
|
|
# NOTE: this mapping is incomplete. If your language is missing, please
|
|
|
|
# submit a bug report to Python bug manager, which you can find via:
|
|
|
|
# http://www.python.org/dev/
|
|
|
|
# Make sure you include the missing language identifier and the suggested
|
|
|
|
# locale code.
|
|
|
|
#
|
2000-07-10 01:12:58 +08:00
|
|
|
|
|
|
|
windows_locale = {
|
|
|
|
0x0404: "zh_TW", # Chinese (Taiwan)
|
|
|
|
0x0804: "zh_CN", # Chinese (PRC)
|
|
|
|
0x0406: "da_DK", # Danish
|
|
|
|
0x0413: "nl_NL", # Dutch (Netherlands)
|
|
|
|
0x0409: "en_US", # English (United States)
|
|
|
|
0x0809: "en_UK", # English (United Kingdom)
|
|
|
|
0x0c09: "en_AU", # English (Australian)
|
|
|
|
0x1009: "en_CA", # English (Canadian)
|
|
|
|
0x1409: "en_NZ", # English (New Zealand)
|
|
|
|
0x1809: "en_IE", # English (Ireland)
|
|
|
|
0x1c09: "en_ZA", # English (South Africa)
|
|
|
|
0x040b: "fi_FI", # Finnish
|
|
|
|
0x040c: "fr_FR", # French (Standard)
|
|
|
|
0x080c: "fr_BE", # French (Belgian)
|
|
|
|
0x0c0c: "fr_CA", # French (Canadian)
|
|
|
|
0x100c: "fr_CH", # French (Switzerland)
|
|
|
|
0x0407: "de_DE", # German (Standard)
|
|
|
|
0x0408: "el_GR", # Greek
|
|
|
|
0x040d: "iw_IL", # Hebrew
|
|
|
|
0x040f: "is_IS", # Icelandic
|
|
|
|
0x0410: "it_IT", # Italian (Standard)
|
|
|
|
0x0411: "ja_JA", # Japanese
|
|
|
|
0x0414: "no_NO", # Norwegian (Bokmal)
|
|
|
|
0x0816: "pt_PT", # Portuguese (Standard)
|
|
|
|
0x0c0a: "es_ES", # Spanish (Modern Sort)
|
|
|
|
0x0441: "sw_KE", # Swahili (Kenya)
|
|
|
|
0x041d: "sv_SE", # Swedish
|
|
|
|
0x081d: "sv_FI", # Swedish (Finland)
|
|
|
|
0x041f: "tr_TR", # Turkish
|
|
|
|
}
|
|
|
|
|
2000-06-07 17:11:40 +08:00
|
|
|
def _print_locale():
|
|
|
|
|
|
|
|
""" Test function.
|
|
|
|
"""
|
|
|
|
categories = {}
|
|
|
|
def _init_categories(categories=categories):
|
|
|
|
for k,v in globals().items():
|
|
|
|
if k[:3] == 'LC_':
|
|
|
|
categories[k] = v
|
|
|
|
_init_categories()
|
|
|
|
del categories['LC_ALL']
|
|
|
|
|
2000-07-10 01:12:58 +08:00
|
|
|
print 'Locale defaults as determined by getdefaultlocale():'
|
2000-06-07 17:11:40 +08:00
|
|
|
print '-'*72
|
2000-07-10 01:12:58 +08:00
|
|
|
lang, enc = getdefaultlocale()
|
2000-06-07 17:11:40 +08:00
|
|
|
print 'Language: ', lang or '(undefined)'
|
|
|
|
print 'Encoding: ', enc or '(undefined)'
|
|
|
|
print
|
|
|
|
|
|
|
|
print 'Locale settings on startup:'
|
|
|
|
print '-'*72
|
|
|
|
for name,category in categories.items():
|
2000-07-10 01:12:58 +08:00
|
|
|
print name, '...'
|
|
|
|
lang, enc = getlocale(category)
|
2000-06-07 17:11:40 +08:00
|
|
|
print ' Language: ', lang or '(undefined)'
|
|
|
|
print ' Encoding: ', enc or '(undefined)'
|
|
|
|
print
|
|
|
|
|
|
|
|
print
|
2000-07-10 01:12:58 +08:00
|
|
|
print 'Locale settings after calling resetlocale():'
|
2000-06-07 17:11:40 +08:00
|
|
|
print '-'*72
|
2000-07-10 01:12:58 +08:00
|
|
|
resetlocale()
|
2000-06-07 17:11:40 +08:00
|
|
|
for name,category in categories.items():
|
2000-07-10 01:12:58 +08:00
|
|
|
print name, '...'
|
|
|
|
lang, enc = getlocale(category)
|
2000-06-07 17:11:40 +08:00
|
|
|
print ' Language: ', lang or '(undefined)'
|
|
|
|
print ' Encoding: ', enc or '(undefined)'
|
|
|
|
print
|
2000-07-10 01:12:58 +08:00
|
|
|
|
2000-06-07 17:11:40 +08:00
|
|
|
try:
|
2000-07-10 01:12:58 +08:00
|
|
|
setlocale(LC_ALL, "")
|
2000-06-07 17:11:40 +08:00
|
|
|
except:
|
|
|
|
print 'NOTE:'
|
2000-07-10 01:12:58 +08:00
|
|
|
print 'setlocale(LC_ALL, "") does not support the default locale'
|
2000-06-07 17:11:40 +08:00
|
|
|
print 'given in the OS environment variables.'
|
|
|
|
else:
|
|
|
|
print
|
2000-07-10 01:12:58 +08:00
|
|
|
print 'Locale settings after calling setlocale(LC_ALL, ""):'
|
2000-06-07 17:11:40 +08:00
|
|
|
print '-'*72
|
|
|
|
for name,category in categories.items():
|
2000-07-10 01:12:58 +08:00
|
|
|
print name, '...'
|
|
|
|
lang, enc = getlocale(category)
|
2000-06-07 17:11:40 +08:00
|
|
|
print ' Language: ', lang or '(undefined)'
|
|
|
|
print ' Encoding: ', enc or '(undefined)'
|
|
|
|
print
|
2000-07-10 01:12:58 +08:00
|
|
|
|
2000-06-07 17:11:40 +08:00
|
|
|
###
|
1997-11-20 03:01:43 +08:00
|
|
|
|
2001-01-24 18:13:46 +08:00
|
|
|
try:
|
|
|
|
LC_MESSAGES
|
2002-03-26 05:40:36 +08:00
|
|
|
except NameError:
|
2001-01-24 18:13:46 +08:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
__all__.append("LC_MESSAGES")
|
|
|
|
|
1997-11-20 03:01:43 +08:00
|
|
|
if __name__=='__main__':
|
2000-06-07 17:11:40 +08:00
|
|
|
print 'Locale aliasing:'
|
|
|
|
print
|
|
|
|
_print_locale()
|
|
|
|
print
|
|
|
|
print 'Number formatting:'
|
|
|
|
print
|
|
|
|
_test()
|