Пример #1
0
Файл: locale.py Проект: Reve/eve
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)
Пример #2
0
    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')
Пример #3
0
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)
Пример #4
0
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)
Пример #5
0
 def getpreferredencoding(do_setlocale = True):
     """Return the charset that the user is likely using."""
     import _locale
     return _locale._getdefaultlocale()[1]
Пример #6
0
 def getpreferredencoding(do_setlocale=True):
     return _locale._getdefaultlocale()[1]
Пример #7
0
 def getpreferredencoding(do_setlocale=True):
     if sys.flags.utf8_mode:
         return 'UTF-8'
     return _locale._getdefaultlocale()[1]
Пример #8
0
def test_getdefaultlocale():
    result1 = _locale._getdefaultlocale()
    result2 = _locale._getdefaultlocale()
    AreEqual(result1,result2)
Пример #9
0
 def test_getdefaultlocale(self):
     result1 = _locale._getdefaultlocale()
     result2 = _locale._getdefaultlocale()
     self.assertEqual(result1,result2)
Пример #10
0
 def getpreferredencoding(do_setlocale=True):
     if sys.flags.utf8_mode:
         return 'UTF-8'
     return _locale._getdefaultlocale()[1]
Пример #11
0
 def test_getdefaultlocale(self):
     result1 = _locale._getdefaultlocale()
     result2 = _locale._getdefaultlocale()
     self.assertEqual(result1, result2)
Пример #12
0
 def getpreferredencoding(do_setlocale=True):
     """Return the charset that the user is likely using."""
     import _locale
     return _locale._getdefaultlocale()[1]
Пример #13
0
 def getpreferredencoding(do_setlocale = True):
     import _locale
     return _locale._getdefaultlocale()[1]
Пример #14
0
def test_getdefaultlocale():
    result1 = _locale._getdefaultlocale()
    result2 = _locale._getdefaultlocale()
    AreEqual(result1, result2)
Пример #15
0
""" Locale support.
Пример #16
0
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"