kopia lustrzana https://github.com/wagtail/wagtail
Handle get_supported_language_variant returning a language variant not 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 #6547pull/6553/head
rodzic
a888d3383c
commit
236632d815
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 (
|
||||
|
@ -259,3 +260,36 @@ class TestGetSupportedContentLanguageVariant(TestCase):
|
|||
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')
|
||||
|
|
|
@ -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
|
||||
|
|
Ładowanie…
Reference in New Issue