(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.
Returns a normalized locale code for the given locale
        name.
Returns a normalized locale code for the given locale name .
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
    if isinstance(localename, _unicode):
        localename = localename.encode('ascii')
    fullname = localename.translate(_ascii_lower_map)
    if ':' in fullname:
        # ':' is sometimes used as encoding delimiter.
        fullname = fullname.replace(':', '.')
    if '.' in fullname:
        langname, encoding = fullname.split('.')[:2]
        fullname = langname + '.' + encoding
    else:
        langname = fullname
        encoding = ''

    # First lookup: fullname (possibly with encoding)
    norm_encoding = encoding.replace('-', '')
    norm_encoding = norm_encoding.replace('_', '')
    lookup_name = langname + '.' + encoding
    code = locale_alias.get(lookup_name, None)
    if code is not None:
        return code
    #print 'first lookup failed'

    # Second try: langname (without encoding)
    code = locale_alias.get(langname, None)
    if code is not None:
        #print 'langname lookup succeeded'
        if '.' in code:
            langname, defenc = code.split('.')
        else:
            langname = code
            defenc = ''
        if encoding:
            # Convert the encoding to a C lib compatible encoding string
            norm_encoding = encodings.normalize_encoding(encoding)
            #print 'norm encoding: %r' % norm_encoding
            norm_encoding = encodings.aliases.aliases.get(norm_encoding,
                                                          norm_encoding)
            #print 'aliased encoding: %r' % norm_encoding
            encoding = locale_encoding_alias.get(norm_encoding,
                                                 norm_encoding)
        else:
            encoding = defenc
        #print 'found encoding %r' % encoding
        if encoding:
            return langname + '.' + encoding
        else:
            return langname

    else:
        return localename
def normalize ( localename ) : # Normalize the locale name and extract the encoding if isinstance ( localename , _unicode ) : localename = localename . encode ( 'ascii' ) fullname = localename . translate ( _ascii_lower_map ) if ':' in fullname : # ':' is sometimes used as encoding delimiter. fullname = fullname . replace ( ':' , '.' ) if '.' in fullname : langname , encoding = fullname . split ( '.' ) [ : 2 ] fullname = langname + '.' + encoding else : langname = fullname encoding = '' # First lookup: fullname (possibly with encoding) norm_encoding = encoding . replace ( '-' , '' ) norm_encoding = norm_encoding . replace ( '_' , '' ) lookup_name = langname + '.' + encoding code = locale_alias . get ( lookup_name , None ) if code is not None : return code #print 'first lookup failed' # Second try: langname (without encoding) code = locale_alias . get ( langname , None ) if code is not None : #print 'langname lookup succeeded' if '.' in code : langname , defenc = code . split ( '.' ) else : langname = code defenc = '' if encoding : # Convert the encoding to a C lib compatible encoding string norm_encoding = encodings . normalize_encoding ( encoding ) #print 'norm encoding: %r' % norm_encoding norm_encoding = encodings . aliases . aliases . get ( norm_encoding , norm_encoding ) #print 'aliased encoding: %r' % norm_encoding encoding = locale_encoding_alias . get ( norm_encoding , norm_encoding ) else : encoding = defenc #print 'found encoding %r' % encoding if encoding : return langname + '.' + encoding else : return langname else : return localename