def test_parses_well_formatted_strings(self): well_formatted_strings_with_expectations = ( ('en-US', [('en_US', 1.0)]), ('en-US,el-GR,fr', [('en_US', 1.0), ('el_GR', 1.0), ('fr', 1.0)]), ('en-US,el;q=0.8', [('en_US', 1.0), ('el', 0.8)])) for acc_lang, expectations in well_formatted_strings_with_expectations: parsed = locales.parse_accept_language(acc_lang) self.assertEquals(expectations, parsed)
def get_locale_for(self, request, app_context, student=None, prefs=None): """Returns a locale that should be used by this request.""" hl = request.get('hl') if hl and hl in self.app_context.get_allowed_locales(): return hl if self.get_user(): # check if student has any locale labels assigned if student is None: student = self.get_student() if student and student.is_enrolled and not student.is_transient: student_label_ids = student.get_labels_of_type( models.LabelDTO.LABEL_TYPE_LOCALE) if student_label_ids: all_labels = models.LabelDAO.get_all_of_type( models.LabelDTO.LABEL_TYPE_LOCALE) student_locales = [] for label in all_labels: if label.type != models.LabelDTO.LABEL_TYPE_LOCALE: continue if label.id in student_label_ids: student_locales.append(label.title) locale = self._pick_first_valid_locale_from_list( student_locales) if locale: return locale # check if user preferences have been set if prefs is None: prefs = models.StudentPreferencesDAO.load_or_create() if prefs is not None and prefs.locale is not None: return prefs.locale locale_cookie = self.request.cookies.get(GUEST_LOCALE_COOKIE) if locale_cookie and ( locale_cookie in self.app_context.get_allowed_locales()): return locale_cookie # check if accept language has been set accept_langs = request.headers.get('Accept-Language') locale = self._pick_first_valid_locale_from_list( [lang for lang, _ in locales.parse_accept_language(accept_langs)]) if locale: return locale return app_context.default_locale
def get_locale_for(self, request, app_context, student=None, prefs=None): """Returns a locale that should be used by this request.""" hl = request.get('hl') if hl and hl in self.app_context.get_allowed_locales(): return hl if self.get_user(): # check if student has any locale labels assigned if student is None: student = self.get_student() if student and student.is_enrolled and not student.is_transient: student_label_ids = student.get_labels_of_type( models.LabelDTO.LABEL_TYPE_LOCALE) if student_label_ids: all_labels = models.LabelDAO.get_all_of_type( models.LabelDTO.LABEL_TYPE_LOCALE) student_locales = [] for label in all_labels: if label.type != models.LabelDTO.LABEL_TYPE_LOCALE: continue if label.id in student_label_ids: student_locales.append(label.title) locale = self._pick_first_valid_locale_from_list( student_locales) if locale: return locale # check if user preferences have been set if prefs is None: prefs = models.StudentPreferencesDAO.load_or_create() if prefs is not None and prefs.locale is not None: return prefs.locale locale_cookie = self.request.cookies.get(GUEST_LOCALE_COOKIE) if locale_cookie and (locale_cookie in self.app_context.get_allowed_locales()): return locale_cookie # check if accept language has been set accept_langs = request.headers.get('Accept-Language') locale = self._pick_first_valid_locale_from_list( [lang for lang, _ in locales.parse_accept_language(accept_langs)]) if locale: return locale return app_context.default_locale
def test_rejects_invalid_syntax(self): self.assertEqual( [('el', 1.0), ('fr', 1.0)], locales.parse_accept_language('el,-us,en-,12-34,fr'))
def test_item_split_ignores_whitespace(self): """Expect form xx_XX returned.""" self.assertEqual( [('en_US', 1.0), ('el_GR', 1.0), ('fr', 1.0)], locales.parse_accept_language('en-US, el-gr , fr '))
def test_coerces_case_to_standard_form(self): """Expect form xx_XX returned.""" self.assertEqual( [('en_US', 1.0), ('el_GR', 1.0), ('fr', 1.0)], locales.parse_accept_language('en-us,EL-gr,FR'))
def test_appect_lang_header_length_capped_at_8k(self): with self.assertRaises(AssertionError): locales.parse_accept_language('x' * 8192)
def test_arranges_quality_scores_in_decreasing_order(self): parsed = locales.parse_accept_language('en-US;q=0.8,el;q=1.0') expected = [('el', 1.0), ('en_US', 0.8)] self.assertEquals(expected, parsed)
def test_rejects_invalid_syntax(self): self.assertEqual([('el', 1.0), ('fr', 1.0)], locales.parse_accept_language('el,-us,en-,12-34,fr'))
def test_coerces_case_to_standard_form(self): """Expect form xx_XX returned.""" self.assertEqual([('en_US', 1.0), ('el_GR', 1.0), ('fr', 1.0)], locales.parse_accept_language('en-us,EL-gr,FR'))