- merged setlocale and set_locale. the internal setlocale

function is overridden by a python version which accepts
  *either* a string (old behaviour) or a locale tuple.

- renamed a few methods (for consistency):

        get_locale => getlocale
        get_default_locale => getdefaultlocale
        set_to_default => resetlocale (!)

- the _locale implementation module can now implement
  an optional _getdefaultlocale function.  if that function
  isn't available, a POSIX-based approach is used (checking
  LANG and other environment variables, as usual).

(patch #100765)
This commit is contained in:
Fredrik Lundh 2000-07-09 17:12:58 +00:00
parent c70b4483d2
commit 6c86b99dc1
1 changed files with 159 additions and 92 deletions

View File

@ -11,15 +11,21 @@
"""
import string
import string, sys
### Load C lib locale APIs or use an emulation
# Try importing the _locale module.
#
# If this fails, fall back on a basic 'C' locale emulation.
#
try:
from _locale import *
except ImportError:
# Locale emulation
CHAR_MAX = 127
LC_ALL = 6
LC_COLLATE = 3
@ -155,6 +161,11 @@ def _test():
### Locale name aliasing engine
# Author: Marc-Andre Lemburg, mal@lemburg.com
# Various tweaks by Fredrik Lundh <effbot@telia.com>
# store away the low-level version of setlocale (it's
# overridden below)
_setlocale = setlocale
def normalize(localename):
@ -248,7 +259,7 @@ def _build_localename(localetuple):
else:
return language + '.' + encoding
def get_default(envvars=('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG')):
def getdefaultlocale(envvars=('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG')):
""" Tries to determine the default locale settings and returns
them as tuple (language code, encoding).
@ -271,6 +282,17 @@ def get_default(envvars=('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG')):
be determined.
"""
try:
# check if it's supported by the _locale module
import _locale
code, encoding = _locale._getdefaultlocale()
if sys.platform == "win32" and code and code[:2] == "0x":
# map windows language identifier to language name
code = windows_locale.get(int(code, 0))
return code, encoding
except (ImportError, NameError):
pass
# fall back on POSIX behaviour
import os
lookup = os.environ.get
for variable in envvars:
@ -281,7 +303,10 @@ def get_default(envvars=('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG')):
localename = 'C'
return _parse_localename(localename)
def get_locale(category=LC_CTYPE):
# compatibility
get_default = getdefaultlocale
def getlocale(category=LC_CTYPE):
""" Returns the current setting for the given locale category as
tuple (language code, encoding).
@ -294,34 +319,36 @@ def get_locale(category=LC_CTYPE):
be determined.
"""
localename = setlocale(category)
localename = _setlocale(category)
if category == LC_ALL and ';' in localename:
raise TypeError, 'category LC_ALL is not supported'
return _parse_localename(localename)
def set_locale(localetuple, category=LC_ALL):
def setlocale(category, locale=None):
""" Set the locale according to the localetuple (language code,
encoding) as returned by get_locale() and get_default().
""" Set the locale for the given category. The locale can be
a string, a locale tuple (language code, encoding), or None.
The given codes are passed through the locale aliasing engine
before being given to setlocale() for processing.
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. It defaults
to LC_ALL.
category may be given as one of the LC_* values.
"""
setlocale(category, normalize(_build_localename(localetuple)))
if locale and type(locale) is not type(""):
# convert to string
locale = normalize(_build_localename(locale))
return _setlocale(category, locale)
def set_to_default(category=LC_ALL):
def resetlocale(category=LC_ALL):
""" Sets the locale for category to the default setting.
The default setting is determined by calling
get_default(). category defaults to LC_ALL.
getdefaultlocale(). category defaults to LC_ALL.
"""
setlocale(category, _build_localename(get_default()))
_setlocale(category, _build_localename(getdefaultlocale()))
### Database
#
@ -561,6 +588,46 @@ locale_alias = {
'zh_tw.euc': 'zh_TW.eucTW',
}
#
# this maps windows language identifiers (as used on Windows 95 and
# earlier) to locale strings.
#
# NOTE: this mapping is incomplete. If your language is missing, send
# a note with the missing language identifier and the suggested locale
# code to Fredrik Lundh <effbot@telia.com>. Thanks /F
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
}
def _print_locale():
""" Test function.
@ -573,9 +640,9 @@ def _print_locale():
_init_categories()
del categories['LC_ALL']
print 'Locale defaults as determined by get_default():'
print 'Locale defaults as determined by getdefaultlocale():'
print '-'*72
lang, enc = get_default()
lang, enc = getdefaultlocale()
print 'Language: ', lang or '(undefined)'
print 'Encoding: ', enc or '(undefined)'
print
@ -584,18 +651,18 @@ def _print_locale():
print '-'*72
for name,category in categories.items():
print name, '...'
lang, enc = get_locale(category)
lang, enc = getlocale(category)
print ' Language: ', lang or '(undefined)'
print ' Encoding: ', enc or '(undefined)'
print
set_to_default()
print
print 'Locale settings after calling set_to_default():'
print 'Locale settings after calling resetlocale():'
print '-'*72
resetlocale()
for name,category in categories.items():
print name, '...'
lang, enc = get_locale(category)
lang, enc = getlocale(category)
print ' Language: ', lang or '(undefined)'
print ' Encoding: ', enc or '(undefined)'
print
@ -612,7 +679,7 @@ def _print_locale():
print '-'*72
for name,category in categories.items():
print name, '...'
lang, enc = get_locale(category)
lang, enc = getlocale(category)
print ' Language: ', lang or '(undefined)'
print ' Encoding: ', enc or '(undefined)'
print