Merge "Use system locale when Accept-Language header is not provided"

This commit is contained in:
Jenkins 2013-08-27 02:03:33 +00:00 committed by Gerrit Code Review
commit 5a514b7367
2 changed files with 19 additions and 6 deletions

View File

@ -100,8 +100,7 @@ class RequestTestCase(base.BaseTestCase):
self.assertTrue(self.req.context.is_admin)
def test_best_match_language(self):
# Here we test that we are actually invoking language negotiation
# by webop and also that the default locale always available is en-US
# Test that we are actually invoking language negotiation by webop
request = wsgi.Request.blank('/')
gettextutils.get_available_languages = mock.MagicMock()
gettextutils.get_available_languages.return_value = ['known-language',
@ -109,9 +108,18 @@ class RequestTestCase(base.BaseTestCase):
request.headers['Accept-Language'] = 'known-language'
language = request.best_match_language()
self.assertEqual(language, 'known-language')
# If the Accept-Leader is an unknown language, missing or empty,
# the best match locale should be None
request.headers['Accept-Language'] = 'unknown-language'
language = request.best_match_language()
self.assertEqual(language, 'en_US')
self.assertEqual(language, None)
request.headers['Accept-Language'] = ''
language = request.best_match_language()
self.assertEqual(language, None)
request.headers.pop('Accept-Language')
language = request.best_match_language()
self.assertEqual(language, None)
class ResourceTestCase(base.BaseTestCase):

View File

@ -301,10 +301,15 @@ class Request(webob.Request):
return None
def best_match_language(self):
"""Determine language for returned response."""
"""Determines best available locale from the Accept-Language header.
:returns: the best language match or None if the 'Accept-Language'
header was not available in the request.
"""
if not self.accept_language:
return None
all_languages = gettextutils.get_available_languages('neutron')
return self.accept_language.best_match(all_languages,
default_match='en_US')
return self.accept_language.best_match(all_languages)
@property
def context(self):