i18n get_locale without User (#546)

* fix: #544 return locale None if User is absent

* fix: #544 fixed typing

* fix: #544 User is Optional

* style: minor docs styling

* fix: explicit None return + typing

* fix: typing
This commit is contained in:
Oleg A 2021-04-28 01:28:34 +03:00 committed by GitHub
parent 75e88f173c
commit ba095f0b9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,7 +1,7 @@
import gettext
import os
from contextvars import ContextVar
from typing import Any, Dict, Tuple
from typing import Any, Dict, Tuple, Optional
from babel import Locale
from babel.support import LazyProxy
@ -119,22 +119,24 @@ class I18nMiddleware(BaseMiddleware):
return LazyProxy(self.gettext, singular, plural, n, locale, enable_cache=enable_cache)
# noinspection PyMethodMayBeStatic,PyUnusedLocal
async def get_user_locale(self, action: str, args: Tuple[Any]) -> str:
async def get_user_locale(self, action: str, args: Tuple[Any]) -> Optional[str]:
"""
User locale getter
You can override the method if you want to use different way of getting user language.
You can override the method if you want to use different way of
getting user language.
:param action: event name
:param args: event arguments
:return: locale name
:return: locale name or None
"""
user: types.User = types.User.get_current()
locale: Locale = user.locale
user: Optional[types.User] = types.User.get_current()
locale: Optional[Locale] = user.locale if user else None
if locale:
*_, data = args
language = data['locale'] = locale.language
return language
return None
async def trigger(self, action, args):
"""