Skip to content

Commit

Permalink
Merge pull request #2989 from kyoshino/bug-1164781-spanish-fallback
Browse files Browse the repository at this point in the history
Fix Bug 1164781 - Spanish locales are redirected to /en-US instead of /es when page is not localized
  • Loading branch information
pmac committed May 15, 2015
2 parents a361b93 + 5f51063 commit 11aefc7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
9 changes: 9 additions & 0 deletions bedrock/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ def get_dev_languages():
'sv': 'sv-SE',
}

# Unlocalized pages are usually redirected to the English (en-US) equivalent,
# but sometimes it would be better to offer another locale as fallback. This map
# specifies such cases.
FALLBACK_LOCALES = {
'es-AR': 'es-ES',
'es-CL': 'es-ES',
'es-MX': 'es-ES',
}


def lazy_lang_url_map():
from django.conf import settings
Expand Down
19 changes: 18 additions & 1 deletion lib/l10n_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,30 @@ def render(request, template, context=None, **kwargs):

matched = None

# Look for the user's Accept-Language HTTP header to find another
# locale we can offer
for lang in get_accept_languages(request):
if template_is_active(template, lang):
matched = lang
break

# Check for the fallback locales if the previous look-up doesn't
# work. This is useful especially in the Spanish locale where es-ES
# should be offered as the fallback of es, es-AR, es-CL and es-MX
if not matched:
for lang in get_accept_languages(request):
lang = settings.FALLBACK_LOCALES.get(lang)
if lang and template_is_active(template, lang):
matched = lang
break

# If all the attempts failed, just use en-US, the default locale of
# the site
if not matched:
matched = settings.LANGUAGE_CODE

response = HttpResponseRedirect('/' + '/'.join([
matched or settings.LANGUAGE_CODE,
matched,
split_path(request.get_full_path())[1]
]))

Expand Down
6 changes: 6 additions & 0 deletions lib/l10n_utils/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ def test_firefox(self):
self._test(path, template, 'zu', 'zu,fr;q=0.7,en;q=0.3',
302, '/fr/firefox/new/')

active_mock.reset_mock()
active_mock.side_effect = [False, False, False, False, True]
# Should fallback to one of the site's fallback languages
self._test(path, template, 'es-CL', 'es-CL,es;q=0.7,en;q=0.3',
302, '/es-ES/firefox/new/')


class TestGetAcceptLanguages(TestCase):
def _test(self, accept_lang, list):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## active ##

0 comments on commit 11aefc7

Please sign in to comment.