From 7588653daad02c35545d27f699caeb01400d91d8 Mon Sep 17 00:00:00 2001 From: "Luis A. Garcia" Date: Tue, 20 Aug 2013 19:16:06 +0000 Subject: [PATCH] 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 --- neutron/tests/unit/test_api_v2_resource.py | 14 +++++++++++--- neutron/wsgi.py | 11 ++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/neutron/tests/unit/test_api_v2_resource.py b/neutron/tests/unit/test_api_v2_resource.py index 91ac57117a..4444c75760 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 35c9746560..6bac0f447e 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):