def normalize_languages(opf_languages, mi_languages):
 def parse(x):
  try:
   return parse_lang_code(x)
  except ValueError:
   return None
 opf_languages = filter(None, map(parse, opf_languages))
 cc_map = {c.langcode: c.countrycode for c in opf_languages}
 mi_languages = filter(None, map(parse, mi_languages))
 def norm(x):
  lc = x.langcode
  cc = (x.countrycode or cc_map.get(lc, None))
  lc = (lang_as_iso639_1(lc) or lc)
  if cc:
   lc += (u'-' + cc)
  return lc
 return list(map(norm, mi_languages))

def normalize_languages ( opf_languages , mi_languages ) : def parse ( x ) : try : return parse_lang_code ( x ) except ValueError : return None opf_languages = filter ( None , map ( parse , opf_languages ) ) cc_map = { c . langcode : c . countrycode for c in opf_languages } mi_languages = filter ( None , map ( parse , mi_languages ) ) def norm ( x ) : lc = x . langcode cc = ( x . countrycode or cc_map . get ( lc , None ) ) lc = ( lang_as_iso639_1 ( lc ) or lc ) if cc : lc += ( u'-' + cc ) return lc return list ( map ( norm , mi_languages ) )
preserve original country codes and use 2-letter lang codes where possible .