SHARE
TWEET

Untitled

a guest Oct 10th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import logging
  2. from pathlib import Path
  3.  
  4. from aiogram import types
  5. from aiogram.contrib.middlewares.i18n import I18nMiddleware
  6. from typing import Tuple, Any, Callable
  7.  
  8. from modules import User
  9. from modules.modificators import override
  10.  
  11. I18N_DOMAIN = 'mybot'
  12. BASE_DIR = Path(__file__).parent
  13. LOCALES_DIR = BASE_DIR / 'locales'
  14.  
  15. """
  16. todo: release i18n in v3.0
  17. """
  18.  
  19. # Setup i18n middleware
  20.  
  21. log = logging.getLogger(__name__)
  22.  
  23.  
  24. class ACLMiddleware(I18nMiddleware):
  25.     def get_tg_lang(self, tg_user: types.User) -> str:
  26.         lang = tg_user.language_code
  27.         if lang:
  28.             lang = lang.split('-')[0]
  29.         else:
  30.             lang = 'en'
  31.         return lang
  32.  
  33.     @override()
  34.     async def get_user_locale(self, action: str, args: Tuple[Any]):
  35.         tg_user = types.User.get_current()
  36.         *_, data = args
  37.         if tg_user is None:
  38.             data['locale'] = 'en'
  39.             return 'en'
  40.         is_new, user = await User.get_user(tg_user)
  41.         args[0].conf['is_new_user'] = is_new
  42.         data['locale'] = user.locale
  43.         data['user'] = user
  44.         lang = user.locale or self.get_tg_lang(tg_user)
  45.         return lang
  46.  
  47.  
  48. i18n = ACLMiddleware(I18N_DOMAIN, LOCALES_DIR)
  49.  
  50. lazy_gettext = i18n.lazy_gettext
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top