fix translation

This commit is contained in:
Dmitriy Kuzmenko 2019-08-28 18:11:56 +03:00
parent 4a8053b231
commit b2a105198f
2 changed files with 7 additions and 4 deletions

View File

@ -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

View File

@ -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: