From: Luis A. Garcia Date: Tue, 20 Aug 2013 19:16:06 +0000 (+0000) Subject: Use system locale when Accept-Language header is not provided X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=d46e3cd59c374b01ff379e63c18cbb16e340fb98;p=openstack-build%2Fneutron-build.git Use system locale when Accept-Language header is not provided Remove en_US as the default language when no header is provided, and use None instead. Upon translation None will be defaulted to system as it was before the translation changes. Fixes bug: #1214476 Change-Id: I0fe22c526710e69ae0731e7d0b42170e6f3a8523 --- diff --git a/neutron/tests/unit/test_api_v2_resource.py b/neutron/tests/unit/test_api_v2_resource.py index 91ac57117..4444c7576 100644 --- a/neutron/tests/unit/test_api_v2_resource.py +++ b/neutron/tests/unit/test_api_v2_resource.py @@ -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): diff --git a/neutron/wsgi.py b/neutron/wsgi.py index 35c974656..6bac0f447 100644 --- a/neutron/wsgi.py +++ b/neutron/wsgi.py @@ -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):