# Buggy code
def get_content_languages():
    """
    Cache of settings.WAGTAIL_CONTENT_LANGUAGES in a dictionary for easy lookups by key.
    """
    content_languages = getattr(settings, "WAGTAIL_CONTENT_LANGUAGES", None)
    languages = dict(settings.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)
        content_languages = [
            (default_language_code, languages[default_language_code]),
        ]

    # Check that each content language is in LANGUAGES
    for language_code, name in content_languages:
        if language_code not in languages:
            raise ImproperlyConfigured(
                "The language {} is specified in WAGTAIL_CONTENT_LANGUAGES but not LANGUAGES. "
                "WAGTAIL_CONTENT_LANGUAGES must be a subset of LANGUAGES.".format(
                    language_code
                )
            )

    return dict(content_languages)


# Traceback (KeyError)
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/matthew/.virtualenvs/use18n/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/Users/matthew/.virtualenvs/use18n/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/matthew/.virtualenvs/use18n/lib/python3.7/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/matthew/.virtualenvs/use18n/lib/python3.7/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/Users/matthew/.virtualenvs/use18n/lib/python3.7/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/Users/matthew/.virtualenvs/use18n/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 92, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/Users/matthew/.virtualenvs/use18n/lib/python3.7/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/Users/matthew/.virtualenvs/use18n/lib/python3.7/site-packages/django/db/migrations/loader.py", line 53, in __init__
self.build_graph()
File "/Users/matthew/.virtualenvs/use18n/lib/python3.7/site-packages/django/db/migrations/loader.py", line 210, in build_graph
self.load_disk()
File "/Users/matthew/.virtualenvs/use18n/lib/python3.7/site-packages/django/db/migrations/loader.py", line 112, in load_disk
migration_module = import_module(migration_path)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/matthew/.virtualenvs/use18n/lib/python3.7/site-packages/wagtail/core/migrations/0056_page_locale_fields_populate.py", line 8, in <module>
class Migration(migrations.Migration):
File "/Users/matthew/.virtualenvs/use18n/lib/python3.7/site-packages/wagtail/core/migrations/0056_page_locale_fields_populate.py", line 15, in Migration
BootstrapTranslatableModel('wagtailcore.Page'),
File "/Users/matthew/.virtualenvs/use18n/lib/python3.7/site-packages/wagtail/core/models.py", line 538, in __init__
language_code = get_supported_content_language_variant(settings.LANGUAGE_CODE)
File "/Users/matthew/.virtualenvs/use18n/lib/python3.7/site-packages/wagtail/core/utils.py", line 261, in get_supported_content_language_variant
supported_lang_codes = get_content_languages()
File "/Users/matthew/.virtualenvs/use18n/lib/python3.7/site-packages/wagtail/core/utils.py", line 224, in get_content_languages
(default_language_code, languages[default_language_code]),
KeyError: 'en-us'

# Fixed code
def get_content_languages():
    """
    Cache of settings.WAGTAIL_CONTENT_LANGUAGES in a dictionary for easy lookups by key.
    """
    content_languages = getattr(settings, "WAGTAIL_CONTENT_LANGUAGES", None)
    languages = dict(settings.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, language_name),
        ]

    # Check that each content language is in LANGUAGES
    for language_code, name in content_languages:
        if language_code not in languages:
            raise ImproperlyConfigured(
                "The language {} is specified in WAGTAIL_CONTENT_LANGUAGES but not LANGUAGES. "
                "WAGTAIL_CONTENT_LANGUAGES must be a subset of LANGUAGES.".format(
                    language_code
                )
            )

    return dict(content_languages)