(localename)
return 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 and modifier
    code = localename.lower()
    if ':' in code:
        # ':' is sometimes used as encoding delimiter.
        code = code.replace(':', '.')
    if '@' in code:
        code, modifier = code.split('@', 1)
    else:
        modifier = ''
    if '.' in code:
        langname, encoding = code.split('.')[:2]
    else:
        langname = code
        encoding = ''

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

    if modifier:
        # Second try: fullname without modifier (possibly with encoding)
        code = locale_alias.get(lang_enc, None)
        if code is not None:
            #print('lookup without modifier succeeded')
            if '@' not in code:
                return _append_modifier(code, modifier)
            if code.split('@', 1)[1].lower() == modifier:
                return code
        #print('second lookup failed')

    if encoding:
        # Third try: langname (without encoding, possibly with modifier)
        lookup_name = langname
        if modifier:
            lookup_name += '@' + modifier
        code = locale_alias.get(lookup_name, None)
        if code is not None:
            #print('lookup without encoding succeeded')
            if '@' not in code:
                return _replace_encoding(code, encoding)
            code, modifier = code.split('@', 1)
            return _replace_encoding(code, encoding) + '@' + modifier

        if modifier:
            # Fourth try: langname (without encoding and modifier)
            code = locale_alias.get(langname, None)
            if code is not None:
                #print('lookup without modifier and encoding succeeded')
                if '@' not in code:
                    code = _replace_encoding(code, encoding)
                    return _append_modifier(code, modifier)
                code, defmod = code.split('@', 1)
                if defmod.lower() == modifier:
                    return _replace_encoding(code, encoding) + '@' + defmod

    return localename
def normalize ( localename ) : # Normalize the locale name and extract the encoding and modifier code = localename . lower ( ) if ':' in code : # ':' is sometimes used as encoding delimiter. code = code . replace ( ':' , '.' ) if '@' in code : code , modifier = code . split ( '@' , 1 ) else : modifier = '' if '.' in code : langname , encoding = code . split ( '.' ) [ : 2 ] else : langname = code encoding = '' # First lookup: fullname (possibly with encoding and modifier) lang_enc = langname if encoding : norm_encoding = encoding . replace ( '-' , '' ) norm_encoding = norm_encoding . replace ( '_' , '' ) lang_enc += '.' + norm_encoding lookup_name = lang_enc if modifier : lookup_name += '@' + modifier code = locale_alias . get ( lookup_name , None ) if code is not None : return code #print('first lookup failed') if modifier : # Second try: fullname without modifier (possibly with encoding) code = locale_alias . get ( lang_enc , None ) if code is not None : #print('lookup without modifier succeeded') if '@' not in code : return _append_modifier ( code , modifier ) if code . split ( '@' , 1 ) [ 1 ] . lower ( ) == modifier : return code #print('second lookup failed') if encoding : # Third try: langname (without encoding, possibly with modifier) lookup_name = langname if modifier : lookup_name += '@' + modifier code = locale_alias . get ( lookup_name , None ) if code is not None : #print('lookup without encoding succeeded') if '@' not in code : return _replace_encoding ( code , encoding ) code , modifier = code . split ( '@' , 1 ) return _replace_encoding ( code , encoding ) + '@' + modifier if modifier : # Fourth try: langname (without encoding and modifier) code = locale_alias . get ( langname , None ) if code is not None : #print('lookup without modifier and encoding succeeded') if '@' not in code : code = _replace_encoding ( code , encoding ) return _append_modifier ( code , modifier ) code , defmod = code . split ( '@' , 1 ) if defmod . lower ( ) == modifier : return _replace_encoding ( code , encoding ) + '@' + defmod return localename