def test_get_language_valid_lang_param(self): """ Should return lang param value if it is in settings.LANGUAGE_URL_MAP """ request = self.factory.get("/?lang=de") eq_(request.GET.get("lang"), "de") ok_("de" in settings.LANGUAGE_URL_MAP) prefixer = Prefixer(request) eq_(prefixer.get_language(), "de")
def test_get_language_valid_lang_param(self): """ Should return lang param value if it is in settings.LANGUAGE_URL_MAP """ request = self.factory.get('/?lang=de') eq_(request.GET.get('lang'), 'de') ok_('de' in settings.LANGUAGE_URL_MAP) prefixer = Prefixer(request) eq_(prefixer.get_language(), 'de')
def test_get_language_invalid_lang_param(self): """ Should return default set by settings.LANGUAGE_CODE if lang param value is not in settings.LANGUAGE_URL_MAP """ request = self.factory.get('/?lang=de') ok_('lang' in request.GET) self.assertFalse('de' in settings.LANGUAGE_URL_MAP) prefixer = Prefixer(request) eq_(prefixer.get_language(), 'en-US')
def test_get_language_default_language_code(self): """ Should return default set by settings.LANGUAGE_CODE if no 'lang' url parameter and no Accept-Language header """ request = self.factory.get('/') self.assertFalse('lang' in request.GET) self.assertFalse(request.META.get('HTTP_ACCEPT_LANGUAGE')) prefixer = Prefixer(request) eq_(prefixer.get_language(), 'en-US')
def test_get_language_invalid_lang_param(self): """ Should return default set by settings.LANGUAGE_CODE if lang param value is not in settings.LANGUAGE_URL_MAP """ request = self.factory.get("/?lang=de") ok_("lang" in request.GET) self.assertFalse("de" in settings.LANGUAGE_URL_MAP) prefixer = Prefixer(request) eq_(prefixer.get_language(), "en-US")
def test_get_language_default_language_code(self): """ Should return default set by settings.LANGUAGE_CODE if no 'lang' url parameter and no Accept-Language header """ request = self.factory.get("/") self.assertFalse("lang" in request.GET) self.assertFalse(request.META.get("HTTP_ACCEPT_LANGUAGE")) prefixer = Prefixer(request) eq_(prefixer.get_language(), "en-US")
def test_get_language_returns_best(self): """ Should pass Accept-Language header value to get_best_language and return result """ request = self.factory.get('/') request.META['HTTP_ACCEPT_LANGUAGE'] = 'de, es' prefixer = Prefixer(request) prefixer.get_best_language = Mock(return_value='de') eq_(prefixer.get_language(), 'de') prefixer.get_best_language.assert_called_once_with('de, es')
def test_get_language_no_best(self): """ Should return default set by settings.LANGUAGE_CODE if get_best_language return value is None """ request = self.factory.get('/') request.META['HTTP_ACCEPT_LANGUAGE'] = 'de, es' prefixer = Prefixer(request) prefixer.get_best_language = Mock(return_value=None) eq_(prefixer.get_language(), 'en-US') prefixer.get_best_language.assert_called_once_with('de, es')
def test_get_language_returns_best(self): """ Should pass Accept-Language header value to get_best_language and return result """ request = self.factory.get("/") request.META["HTTP_ACCEPT_LANGUAGE"] = "de, es" prefixer = Prefixer(request) prefixer.get_best_language = Mock(return_value="de") eq_(prefixer.get_language(), "de") prefixer.get_best_language.assert_called_once_with("de, es")
def test_get_language_no_best(self): """ Should return default set by settings.LANGUAGE_CODE if get_best_language return value is None """ request = self.factory.get("/") request.META["HTTP_ACCEPT_LANGUAGE"] = "de, es" prefixer = Prefixer(request) prefixer.get_best_language = Mock(return_value=None) eq_(prefixer.get_language(), "en-US") prefixer.get_best_language.assert_called_once_with("de, es")
def process_request(self, request): # Exit early if we are not in the Facebook app section of the site to # avoid clashing with the Django auth middleware. if not in_facebook_app(request): return None # Default to an anonymous user. request.user = AnonymousFacebookUser() locale = None try: user = FacebookUser.objects.get(id=request.session[SESSION_KEY]) request.user = user locale = user.locale except (FacebookUser.DoesNotExist, KeyError): pass if locale is None: # Since we can't get their locale from their user data, we'll use # funfactory's prefixer instead. prefixer = Prefixer(request) locale = prefixer.get_language() activate_locale(request, locale)