def test_request_language_resolution_with_request_header(self): """Test for frappe.translate.get_language Case 5: frappe.form_dict._lang & preferred_language cookie is not set, but Accept-Language header is """ set_request(method="POST", path="/", headers=[("Accept-Language", third_lang)]) return_val = get_language() self.assertNotIn(return_val, [third_lang, get_parent_language(third_lang)])
def test_guest_request_language_resolution_with_cookie(self): """Test for frappe.translate.get_language Case 3: frappe.form_dict._lang is not set, but preferred_language cookie is [Guest User] """ with patch.object(frappe.translate, "get_preferred_language_cookie", return_value=second_lang): set_request(method="POST", path="/", headers=[("Accept-Language", third_lang)]) return_val = get_language() self.assertIn(return_val, [second_lang, get_parent_language(second_lang)])
def test_request_language_resolution_with_cookie(self): """Test for frappe.translate.get_language Case 2: frappe.form_dict._lang is not set, but preferred_language cookie is """ with patch.object(frappe.translate, "get_preferred_language_cookie", return_value="fr"): set_request(method="POST", path="/", headers=[("Accept-Language", "hr")]) return_val = get_language() # system default language self.assertEqual(return_val, "en") self.assertNotIn(return_val, [second_lang, get_parent_language(second_lang)])
def test_request_language_resolution_with_form_dict(self): """Test for frappe.translate.get_language Case 1: frappe.form_dict._lang is set """ frappe.form_dict._lang = first_lang with patch.object(frappe.translate, "get_preferred_language_cookie", return_value=second_lang): return_val = get_language() self.assertIn(return_val, [first_lang, get_parent_language(first_lang)])
def render_page_by_language(path): translated_languages = frappe.get_hooks("translated_languages_for_website") user_lang = get_language(translated_languages) if translated_languages and user_lang in translated_languages: try: if path and path != "index": lang_path = "{0}/{1}".format(user_lang, path) else: lang_path = user_lang # index return render_page(lang_path) except frappe.DoesNotExistError: return render_page(path) else: return render_page(path)
def set_lang(self): frappe.local.lang = get_language()