]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Use system locale when Accept-Language header is not provided
authorLuis A. Garcia <luis@linux.vnet.ibm.com>
Tue, 20 Aug 2013 16:47:38 +0000 (16:47 +0000)
committerLuis A. Garcia <luis@linux.vnet.ibm.com>
Tue, 20 Aug 2013 19:16:01 +0000 (19:16 +0000)
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

cinder/api/openstack/wsgi.py
cinder/tests/api/openstack/test_wsgi.py

index 89a4356ae417966477a7c59dc7bcab2d531ef6a7..046a5601d4d4da3ffe4d21a480f65902e4f098e6 100644 (file)
@@ -104,10 +104,15 @@ class Request(webob.Request):
         return content_type
 
     def best_match_language(self):
-        """Determines best available locale from the Accept-Language header."""
+        """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('cinder')
-        return self.accept_language.best_match(all_languages,
-                                               default_match='en_US')
+        return self.accept_language.best_match(all_languages)
 
 
 class ActionDispatcher(object):
index 0413a842eaabdeab8cb76935c17835d6cc020b2e..69161189ac17d7debbc55e14ecb88e4a89263042 100644 (file)
@@ -88,19 +88,24 @@ class RequestTest(test.TestCase):
         self.assertEqual(result, "application/json")
 
     def test_best_match_language(self):
-        # Here we test that we are actually invoking language negotiation
-        # by webob and also that the default locale always available is en-US
+        # Test that we are actually invoking language negotiation by webob
         request = wsgi.Request.blank('/')
         accepted = 'unknown-lang'
         request.headers = {'Accept-Language': accepted}
 
         def fake_best_match(self, offers, default_match=None):
-            return default_match
+            # Match would return None, if requested lang is not found
+            return None
 
         self.stubs.SmartSet(request.accept_language,
                             'best_match', fake_best_match)
 
-        self.assertEqual(request.best_match_language(), 'en_US')
+        self.assertEqual(request.best_match_language(), None)
+        # If accept-language is not included or empty, match should be None
+        request.headers = {'Accept-Language': ''}
+        self.assertEqual(request.best_match_language(), None)
+        request.headers.pop('Accept-Language')
+        self.assertEqual(request.best_match_language(), None)
 
 
 class ActionDispatcherTest(test.TestCase):