Skip to content

Commit

Permalink
Handle get_supported_language_variant returning a language variant no…
Browse files Browse the repository at this point in the history
…t in LANGUAGES (#6547)

* Handle get_supported_language_variant returning a language variant not in LANGUAGES

Fixes #6539

* Handle LANGUAGE_CODE that isn't in LANGUAGES

* Release note for #6547
  • Loading branch information
gasman committed Nov 16, 2020
1 parent a888d33 commit 236632d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Expand Up @@ -12,6 +12,7 @@ Changelog

* Fix: Improve performance of permission check on translations for edit page (Karl Hobley)
* Fix: Gracefully handle missing Locale records on `Locale.get_active` and `.localized` (Matt Westcott)
* Fix: Handle `get_supported_language_variant` returning a language variant not in `LANGUAGES` (Matt Westcott)


2.11.1 (06.11.2020)
Expand Down
1 change: 1 addition & 0 deletions docs/releases/2.11.2.rst
Expand Up @@ -15,3 +15,4 @@ Bug fixes

* Improve performance of permission check on translations for edit page (Karl Hobley)
* Gracefully handle missing Locale records on ``Locale.get_active`` and ``.localized`` (Matt Westcott)
* Handle ``get_supported_language_variant`` returning a language variant not in ``LANGUAGES`` (Matt Westcott)
34 changes: 34 additions & 0 deletions wagtail/core/tests/test_utils.py
Expand Up @@ -2,6 +2,7 @@
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase, override_settings
from django.utils.text import slugify
from django.utils.translation import _trans

from wagtail.core.models import Page
from wagtail.core.utils import (
Expand Down Expand Up @@ -259,3 +260,36 @@ def test_uses_wagtail_content_languages(self):
g('xyz')
with self.assertRaises(LookupError):
g('xy-zz')


@override_settings(
USE_I18N=False,
WAGTAIL_I18N_ENABLED=False,
WAGTAIL_CONTENT_LANGUAGES=None,
LANGUAGE_CODE='en-us',
)
class TestGetSupportedContentLanguageVariantWithI18nFalse(TestCase):
def setUp(self):
# Need to forcibly clear the django.utils.translation._trans object when overriding
# USE_I18N:
# https://github.com/django/django/blob/3.1/django/utils/translation/__init__.py#L46-L48
_trans.__dict__.clear()

def tearDown(self):
_trans.__dict__.clear()

def test_lookup_language_with_i18n_false(self):
# Make sure we can handle the 'null' USE_I18N=False implementation of
# get_supported_language_variant returning 'en-us' rather than 'en',
# despite 'en-us' not being in LANGUAGES.
# https://github.com/wagtail/wagtail/issues/6539

self.assertEqual(get_supported_content_language_variant('en-us'), 'en')

@override_settings(LANGUAGE_CODE='zz')
def test_language_code_not_in_languages(self):
# Ensure we can handle a LANGUAGE_CODE setting that isn't defined in LANGUAGES -
# in this case get_content_languages has to cope with not being able to retrieve
# a display name for the language
self.assertEqual(get_supported_content_language_variant('zz'), 'zz')
self.assertEqual(get_supported_content_language_variant('zz-gb'), 'zz')
17 changes: 16 additions & 1 deletion wagtail/core/utils.py
Expand Up @@ -220,8 +220,23 @@ def get_content_languages():
if content_languages is None:
# Default to a single language based on LANGUAGE_CODE
default_language_code = get_supported_language_variant(settings.LANGUAGE_CODE)
try:
language_name = languages[default_language_code]
except KeyError:
# get_supported_language_variant on the 'null' translation backend (used for
# USE_I18N=False) returns settings.LANGUAGE_CODE unchanged without accounting for
# language variants (en-us versus en), so retry with the generic version.
default_language_code = default_language_code.split("-")[0]
try:
language_name = languages[default_language_code]
except KeyError:
# Can't extract a display name, so fall back on displaying LANGUAGE_CODE instead
language_name = settings.LANGUAGE_CODE
# Also need to tweak the languages dict to get around the check below
languages[default_language_code] = settings.LANGUAGE_CODE

content_languages = [
(default_language_code, languages[default_language_code]),
(default_language_code, language_name),
]

# Check that each content language is in LANGUAGES
Expand Down

0 comments on commit 236632d

Please sign in to comment.