fix translation
This commit is contained in:
parent
4a8053b231
commit
b2a105198f
|
|
@ -1,6 +1,8 @@
|
||||||
"""Custom middleware."""
|
"""Custom middleware."""
|
||||||
from django.utils import translation
|
from django.utils import translation
|
||||||
from configuration.models import TranslationSettings
|
from configuration.models import TranslationSettings
|
||||||
|
from translation.models import Language
|
||||||
|
|
||||||
|
|
||||||
def get_locale(cookie_dict):
|
def get_locale(cookie_dict):
|
||||||
return cookie_dict.get('locale')
|
return cookie_dict.get('locale')
|
||||||
|
|
@ -14,9 +16,10 @@ def parse_cookies(get_response):
|
||||||
"""Parse cookies."""
|
"""Parse cookies."""
|
||||||
def middleware(request):
|
def middleware(request):
|
||||||
cookie_dict = request.COOKIES
|
cookie_dict = request.COOKIES
|
||||||
|
|
||||||
# processing locale cookie
|
# 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)
|
translation.activate(locale)
|
||||||
request.locale = locale
|
request.locale = locale
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,9 +36,9 @@ def to_locale(language):
|
||||||
if not country:
|
if not country:
|
||||||
return language
|
return language
|
||||||
# A language with > 2 characters after the dash only has its first
|
# 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
|
# 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, _, tail = country.partition('-')
|
||||||
country = country.title() if len(country) > 2 else country.upper()
|
country = country.title() if len(country) > 2 else country.upper()
|
||||||
if tail:
|
if tail:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user