Ejemplo n.º 1
0
 def test_wash_languages(self):
     """messages - washing multiple languages"""
     if "de" not in CFG_SITE_LANGS:
         self.assertEqual(messages.wash_languages(["00", "11", "22", "de"]), CFG_SITE_LANG)
     else:
         self.assertEqual(messages.wash_languages(["00", "11", "22", "de"]), "de")
     self.assertEqual(messages.wash_languages(["00", "11", "22"]), CFG_SITE_LANG)
Ejemplo n.º 2
0
 def test_wash_languages(self):
     """messages - washing multiple languages"""
     if 'de' not in CFG_SITE_LANGS:
         self.assertEqual(messages.wash_languages(['00', '11', '22', 'de']),
                          CFG_SITE_LANG)
     else:
         self.assertEqual(messages.wash_languages(['00', '11', '22', 'de']),
                          'de')
     self.assertEqual(messages.wash_languages(['00', '11', '22']),
                      CFG_SITE_LANG)
Ejemplo n.º 3
0
def get_preferred_user_language(req):
    def _get_language_from_req_header(accept_language_header):
        """Extract langs info from req.headers_in['Accept-Language'] which
        should be set to something similar to:
        'fr,en-us;q=0.7,en;q=0.3'
        """
        tmp_langs = {}
        for lang in accept_language_header.split(','):
            lang = lang.split(';q=')
            if len(lang) == 2:
                lang[1] = lang[1].replace('"', '')  # Hack for Yeti robot
                try:
                    tmp_langs[float(lang[1])] = lang[0]
                except ValueError:
                    pass
            else:
                tmp_langs[1.0] = lang[0]
        ret = []
        priorities = tmp_langs.keys()
        priorities.sort()
        priorities.reverse()
        for priority in priorities:
            ret.append(tmp_langs[priority])
        return ret

    uid = getUid(req)
    guest = isGuestUser(uid)
    new_lang = None
    preferred_lang = None

    if not guest:
        user_preferences = get_user_preferences(uid)
        preferred_lang = new_lang = user_preferences.get('language', None)

    if not new_lang:
        try:
            new_lang = wash_languages(cgi.parse_qs(req.args)['ln'])
        except (TypeError, AttributeError, KeyError):
            pass

    if not new_lang:
        try:
            new_lang = wash_languages(
                _get_language_from_req_header(
                    req.headers_in['Accept-Language']))
        except (TypeError, AttributeError, KeyError):
            pass

    new_lang = wash_language(new_lang)

    if new_lang != preferred_lang and not guest:
        user_preferences['language'] = new_lang
        set_user_preferences(uid, user_preferences)

    return new_lang
Ejemplo n.º 4
0
def get_preferred_user_language(req):
    def _get_language_from_req_header(accept_language_header):
        """Extract langs info from req.headers_in['Accept-Language'] which
        should be set to something similar to:
        'fr,en-us;q=0.7,en;q=0.3'
        """
        tmp_langs = {}
        for lang in accept_language_header.split(','):
            lang = lang.split(';q=')
            if len(lang) == 2:
                lang[1] = lang[1].replace('"', '') # Hack for Yeti robot
                try:
                    tmp_langs[float(lang[1])] = lang[0]
                except ValueError:
                    pass
            else:
                tmp_langs[1.0] = lang[0]
        ret = []
        priorities = tmp_langs.keys()
        priorities.sort()
        priorities.reverse()
        for priority in priorities:
            ret.append(tmp_langs[priority])
        return ret

    uid = getUid(req)
    guest = isGuestUser(uid)
    new_lang = None
    preferred_lang = None

    if not guest:
        user_preferences = get_user_preferences(uid)
        preferred_lang = new_lang = user_preferences.get('language', None)

    if not new_lang:
        try:
            new_lang = wash_languages(cgi.parse_qs(req.args)['ln'])
        except (TypeError, AttributeError, KeyError):
            pass

    if not new_lang:
        try:
            new_lang = wash_languages(_get_language_from_req_header(req.headers_in['Accept-Language']))
        except (TypeError, AttributeError, KeyError):
            pass

    new_lang = wash_language(new_lang)

    if new_lang != preferred_lang and not guest:
        user_preferences['language'] = new_lang
        set_user_preferences(uid, user_preferences)

    return new_lang
Ejemplo n.º 5
0
 def test_wash_languages(self):
     """messages - washing multiple languages"""
     if 'de' not in CFG_SITE_LANGS:
         self.assertEqual(messages.wash_languages(['00',
                                               '11',
                                               '22',
                                               'de']),
                      CFG_SITE_LANG)
     else:
         self.assertEqual(messages.wash_languages(['00',
                                               '11',
                                               '22',
                                               'de']),
                      'de')
     self.assertEqual(messages.wash_languages(['00',
                                               '11',
                                               '22']),
                      CFG_SITE_LANG)