from django.utils.translation import get_language

from wagtail.users.models import UserProfile


# This patch is needed because this commit:
# https://github.com/wagtail/wagtail/pull/13047/commits/c91bff5d21f43a1d17fe8a8f964def5e83800dd1
# This commit breaks our language selector in the admin. As it prevents setting a language that's not in
# the 'WAGTAILADMIN_PERMITTED_LANGUAGES' setting and it would default to the original language if done so.
# However, we must be able to switch to languages set in the Django `LANGUAGES` setting because this is how
# our ocyan.i18n package works.
def patched_get_preferred_language(self):  # pylint: disable=unused-argument
    return get_language()


UserProfile.get_preferred_language = patched_get_preferred_language
