def getdefaultlocale(envvars=("LC_ALL", "LC_CTYPE", "LANG", "LANGUAGE")): try: import _locale code, encoding = _locale._getdefaultlocale() except (ImportError, AttributeError): pass else: if sys.platform == "win32" and code and code[:2] == "0x": code = windows_locale.get(int(code, 0)) return (code, encoding) import os lookup = os.environ.get for variable in envvars: localename = lookup(variable, None) if localename: if variable == "LANGUAGE": localename = localename.split(":")[0] break else: localename = "C" return _parse_localename(localename)
def test_getdefaultlocale(self): import sys if sys.platform != 'win32': skip("No _getdefaultlocale() to test") import _locale lang, encoding = _locale._getdefaultlocale() assert lang is None or isinstance(lang, str) assert encoding.startswith('cp')
def 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. """ try: # check if it's supported by the _locale module import _locale code, encoding = _locale._getdefaultlocale() except (ImportError, AttributeError): pass else: # make sure the code/encoding values are valid if sys.platform == "win32" and code and code[:2] == "0x": # map windows language identifier to language name code = windows_locale.get(int(code, 0)) # ...add other platform-specific processing here, if # necessary... return code, encoding # fall back on POSIX behaviour import os lookup = os.environ.get for variable in envvars: localename = lookup(variable,None) if localename: if variable == 'LANGUAGE': localename = localename.split(':')[0] break else: localename = 'C' return _parse_localename(localename)
def getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')): try: import _locale (code, encoding) = _locale._getdefaultlocale() except (ImportError, AttributeError): pass if sys.platform == 'win32' and code and code[:2] == '0x': code = windows_locale.get(int(code, 0)) return (code, encoding) import os lookup = os.environ.get for variable in envvars: localename = lookup(variable, None) while localename: if variable == 'LANGUAGE': localename = localename.split(':')[0] break localename = 'C' return _parse_localename(localename)
def getpreferredencoding(do_setlocale = True): """Return the charset that the user is likely using.""" import _locale return _locale._getdefaultlocale()[1]
def getpreferredencoding(do_setlocale=True): return _locale._getdefaultlocale()[1]
def getpreferredencoding(do_setlocale=True): if sys.flags.utf8_mode: return 'UTF-8' return _locale._getdefaultlocale()[1]
def test_getdefaultlocale(): result1 = _locale._getdefaultlocale() result2 = _locale._getdefaultlocale() AreEqual(result1,result2)
def test_getdefaultlocale(self): result1 = _locale._getdefaultlocale() result2 = _locale._getdefaultlocale() self.assertEqual(result1,result2)
def test_getdefaultlocale(self): result1 = _locale._getdefaultlocale() result2 = _locale._getdefaultlocale() self.assertEqual(result1, result2)
def getpreferredencoding(do_setlocale=True): """Return the charset that the user is likely using.""" import _locale return _locale._getdefaultlocale()[1]
def getpreferredencoding(do_setlocale = True): import _locale return _locale._getdefaultlocale()[1]
def test_getdefaultlocale(): result1 = _locale._getdefaultlocale() result2 = _locale._getdefaultlocale() AreEqual(result1, result2)
""" Locale support.
import os def local_environ(): vars = ['LANGUAGE', 'LC_ALL', 'LC_TYPE', 'LANG'] for v in vars: loc = os.environ.get(v) if loc: return loc else: return None ret = local_environ() print ret import _locale code, encoding = _locale._getdefaultlocale() print code print encoding print "Code: %r, Encoding: %s" % (code, encoding) print "end ... \n"