Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Jenkins authored and Luis A. Garcia committed Aug 20, 2013
1 parent 1623b03 commit 95bffe0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
11 changes: 8 additions & 3 deletions heat/common/wsgi.py
Expand Up @@ -426,10 +426,15 @@ def get_content_type(self, allowed_content_types):
return content_type

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('heat')
return self.accept_language.best_match(all_languages,
default_match='en_US')
return self.accept_language.best_match(all_languages)


def is_json_content_type(request):
Expand Down
14 changes: 10 additions & 4 deletions heat/tests/test_wsgi.py
Expand Up @@ -80,19 +80,25 @@ def test_content_type_accept_default(self):
self.assertEqual(result, "application/json")

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('/')
accepted = 'unknown-lang'
request.headers = {'Accept-Language': accepted}

def fake_best_match(self, offers, default_match=None):
return default_match
# Best match on an unknown locale returns None
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 missing 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 ResourceTest(HeatTestCase):
Expand Down

0 comments on commit 95bffe0

Please sign in to comment.