def negotiate_locale(preferred, available, sep='_', aliases=LOCALE_ALIASES):
 available = [a.lower() for a in available if a]
 for locale in preferred:
  ll = locale.lower()
  if (ll in available):
   return locale
  if aliases:
   alias = aliases.get(ll)
   if alias:
    alias = alias.replace('_', sep)
    if (alias.lower() in available):
     return alias
  parts = locale.split(sep)
  if ((len(parts) > 1) and (parts[0].lower() in available)):
   return parts[0]
 return None

def negotiate_locale ( preferred , available , sep = '_' , aliases = LOCALE_ALIASES ) : available = [ a . lower ( ) for a in available if a ] for locale in preferred : ll = locale . lower ( ) if ( ll in available ) : return locale if aliases : alias = aliases . get ( ll ) if alias : alias = alias . replace ( '_' , sep ) if ( alias . lower ( ) in available ) : return alias parts = locale . split ( sep ) if ( ( len ( parts ) > 1 ) and ( parts [ 0 ] . lower ( ) in available ) ) : return parts [ 0 ] return None
find the best match between available and requested locale strings .