diff --git a/apps/utils/middleware.py b/apps/utils/middleware.py index 855c4f9e..398e4437 100644 --- a/apps/utils/middleware.py +++ b/apps/utils/middleware.py @@ -1,6 +1,8 @@ """Custom middleware.""" from django.utils import translation from configuration.models import TranslationSettings +from translation.models import Language + def get_locale(cookie_dict): return cookie_dict.get('locale') @@ -14,9 +16,10 @@ def parse_cookies(get_response): """Parse cookies.""" def middleware(request): cookie_dict = request.COOKIES - # processing locale cookie - locale = get_locale(cookie_dict) or TranslationSettings.get_solo().default_language + locale = get_locale(cookie_dict) + if not Language.objects.filter(locale=locale).exists(): + locale = TranslationSettings.get_solo().default_language translation.activate(locale) request.locale = locale diff --git a/apps/utils/models.py b/apps/utils/models.py index b9ed82a6..0e7fdd7f 100644 --- a/apps/utils/models.py +++ b/apps/utils/models.py @@ -36,9 +36,9 @@ def to_locale(language): if not country: return language # A language with > 2 characters after the dash only has its first - # character after the dash capitalized; e.g. sr-latn becomes sr_Latn. + # character after the dash capitalized; e.g. sr-latn becomes sr-Latn. # A language with 2 characters after the dash has both characters - # capitalized; e.g. en-us becomes en_US. + # capitalized; e.g. en-us becomes en-US. country, _, tail = country.partition('-') country = country.title() if len(country) > 2 else country.upper() if tail: