'If a user\'s UserPreference contains a language preference, use the user\'s preference.
Save the current language preference cookie as the user\'s preferred language.'

def process_request(self, request):

    cookie_lang = request.COOKIES.get(settings.LANGUAGE_COOKIE, None)
    if cookie_lang:
        if request.user.is_authenticated():
            set_user_preference(request.user, LANGUAGE_KEY, cookie_lang)
        else:
            request._anonymous_user_cookie_lang = cookie_lang
        accept_header = request.META.get(LANGUAGE_HEADER, None)
        if accept_header:
            current_langs = parse_accept_lang_header(accept_header)
            current_langs = [(lang, qvalue) for (lang, qvalue) in current_langs if (lang != cookie_lang)]
            current_langs.insert(0, (cookie_lang, 1))
            accept_header = ','.join(('{};q={}'.format(lang, qvalue) for (lang, qvalue) in current_langs))
        else:
            accept_header = cookie_lang
        request.META[LANGUAGE_HEADER] = accept_header
        if ((LANGUAGE_SESSION_KEY in request.session) and (request.session[LANGUAGE_SESSION_KEY] != cookie_lang)):
            del request.session[LANGUAGE_SESSION_KEY]
