'Given an Accept-Language header, return the best-matching language.'

def get_best_language(self, accept_lang):

    lum = settings.LANGUAGE_URL_MAP
    langs = dict((lum.items() + settings.CANONICAL_LOCALES.items()))
    langs.update(((k.split('-')[0], v) for (k, v) in lum.items() if (k.split('-')[0] not in langs)))
    try:
        ranked = parse_accept_lang_header(accept_lang)
    except ValueError:
        return
    else:
        for (lang, _) in ranked:
            lang = lang.lower()
            if (lang in langs):
                return langs[lang]
            pre = lang.split('-')[0]
            if (pre in langs):
                return langs[pre]
