'Determines the user\'s locale from ``Accept-Language`` header.
See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4'

def get_browser_locale(self, default='en_US'):

    if ('Accept-Language' in self.request.headers):
        languages = self.request.headers['Accept-Language'].split(',')
        locales = []
        for language in languages:
            parts = language.strip().split(';')
            if ((len(parts) > 1) and parts[1].startswith('q=')):
                try:
                    score = float(parts[1][2:])
                except (ValueError, TypeError):
                    score = 0.0
            else:
                score = 1.0
            locales.append((parts[0], score))
        if locales:
            locales.sort(key=(lambda pair: pair[1]), reverse=True)
            codes = [l[0] for l in locales]
            return locale.get(*codes)
    return locale.get(default)
