Refactor language and role enums; update user language default value
This commit is contained in:
@@ -66,7 +66,7 @@ class AppEntity(BotEntity):
|
|||||||
back_populates="entities",
|
back_populates="entities",
|
||||||
sa_relationship_kwargs={
|
sa_relationship_kwargs={
|
||||||
"lazy": "selectin",
|
"lazy": "selectin",
|
||||||
"foreign_keys": "Entity.user_id",
|
"foreign_keys": "AppEntity.user_id",
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
caption="User",
|
caption="User",
|
||||||
|
|||||||
@@ -1,25 +1,51 @@
|
|||||||
from .main import QuickBot as QuickBot, Config as Config
|
from .main import QuickBot, Config
|
||||||
from .router import Router as Router
|
from .router import Router
|
||||||
from .model.bot_entity import BotEntity as BotEntity
|
from .model.bot_entity import BotEntity
|
||||||
from .model.bot_process import BotProcess as BotProcess
|
from .model.bot_process import BotProcess
|
||||||
from .model.bot_enum import BotEnum as BotEnum, EnumMember as EnumMember
|
from .model import BotEnum, EnumMember
|
||||||
from .bot.handlers.context import (
|
from .bot.handlers.context import (
|
||||||
ContextData as ContextData,
|
ContextData,
|
||||||
CallbackCommand as CallbackCommand,
|
CallbackCommand,
|
||||||
CommandContext as CommandContext,
|
CommandContext,
|
||||||
)
|
)
|
||||||
from .model.descriptors import (
|
from .model.descriptors import (
|
||||||
Entity as Entity,
|
Entity,
|
||||||
EntityField as EntityField,
|
EntityField,
|
||||||
EntityForm as EntityForm,
|
EntityForm,
|
||||||
EntityList as EntityList,
|
EntityList,
|
||||||
Filter as Filter,
|
Filter,
|
||||||
EntityPermission as EntityPermission,
|
EntityPermission,
|
||||||
CommandCallbackContext as CommandCallbackContext,
|
CommandCallbackContext,
|
||||||
BotContext as BotContext,
|
BotContext,
|
||||||
CommandButton as CommandButton,
|
CommandButton,
|
||||||
FieldEditButton as FieldEditButton,
|
FieldEditButton,
|
||||||
InlineButton as InlineButton,
|
InlineButton,
|
||||||
FormField as FormField,
|
FormField,
|
||||||
Process as Process,
|
Process,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"QuickBot",
|
||||||
|
"Config",
|
||||||
|
"Router",
|
||||||
|
"BotEntity",
|
||||||
|
"BotProcess",
|
||||||
|
"BotEnum",
|
||||||
|
"EnumMember",
|
||||||
|
"ContextData",
|
||||||
|
"CallbackCommand",
|
||||||
|
"CommandContext",
|
||||||
|
"Entity",
|
||||||
|
"EntityField",
|
||||||
|
"EntityForm",
|
||||||
|
"EntityList",
|
||||||
|
"Filter",
|
||||||
|
"EntityPermission",
|
||||||
|
"CommandCallbackContext",
|
||||||
|
"BotContext",
|
||||||
|
"CommandButton",
|
||||||
|
"FieldEditButton",
|
||||||
|
"InlineButton",
|
||||||
|
"FormField",
|
||||||
|
"Process",
|
||||||
|
]
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ async def default_start_handler[UserType: UserBase](
|
|||||||
]:
|
]:
|
||||||
lang = LanguageBase(message.from_user.language_code)
|
lang = LanguageBase(message.from_user.language_code)
|
||||||
else:
|
else:
|
||||||
lang = LanguageBase.EN
|
lang = LanguageBase.DEFAULT
|
||||||
|
|
||||||
user = await User.create(
|
user = await User.create(
|
||||||
session=db_session,
|
session=db_session,
|
||||||
|
|||||||
40
src/quickbot/i18n/__init__.py
Normal file
40
src/quickbot/i18n/__init__.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import gettext as gt
|
||||||
|
import os
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
|
||||||
|
def get_translator(language, domain, localedir):
|
||||||
|
return gt.translation(domain, localedir, languages=[language])
|
||||||
|
|
||||||
|
|
||||||
|
def gettext(message: str) -> str:
|
||||||
|
return gt.gettext(message)
|
||||||
|
|
||||||
|
|
||||||
|
def get_local_text(message: str, locale: str) -> str:
|
||||||
|
return translators[locale].gettext(message)
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def use_locale(locale: str):
|
||||||
|
original_gettext = gt.gettext
|
||||||
|
gt.gettext = translators[locale].gettext
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
gt.gettext = original_gettext
|
||||||
|
|
||||||
|
|
||||||
|
locales_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, "locales")
|
||||||
|
|
||||||
|
locales = [
|
||||||
|
locale
|
||||||
|
for locale in os.listdir(locales_dir)
|
||||||
|
if os.path.isdir(os.path.join(locales_dir, locale))
|
||||||
|
]
|
||||||
|
|
||||||
|
translators = {
|
||||||
|
locale: get_translator(locale, "messages", locales_dir)
|
||||||
|
for locale in os.listdir(locales_dir)
|
||||||
|
if os.path.isdir(os.path.join(locales_dir, locale))
|
||||||
|
}
|
||||||
@@ -6,6 +6,12 @@ from typing import cast
|
|||||||
from .bot_enum import BotEnum, EnumMember
|
from .bot_enum import BotEnum, EnumMember
|
||||||
from ..db import async_session
|
from ..db import async_session
|
||||||
|
|
||||||
|
from .user import UserBase
|
||||||
|
from .role import RoleBase
|
||||||
|
from .language import LanguageBase
|
||||||
|
|
||||||
|
__all__ = ["UserBase", "RoleBase", "LanguageBase"]
|
||||||
|
|
||||||
|
|
||||||
class EntityPermission(BotEnum):
|
class EntityPermission(BotEnum):
|
||||||
LIST_RLS = EnumMember("list_rls")
|
LIST_RLS = EnumMember("list_rls")
|
||||||
|
|||||||
@@ -2,4 +2,4 @@ from .bot_enum import BotEnum, EnumMember
|
|||||||
|
|
||||||
|
|
||||||
class LanguageBase(BotEnum):
|
class LanguageBase(BotEnum):
|
||||||
EN = EnumMember("en", {"en": "🇬🇧 english"})
|
DEFAULT = EnumMember("default")
|
||||||
|
|||||||
@@ -2,5 +2,5 @@ from .bot_enum import BotEnum, EnumMember
|
|||||||
|
|
||||||
|
|
||||||
class RoleBase(BotEnum):
|
class RoleBase(BotEnum):
|
||||||
SUPER_USER = EnumMember("super_user")
|
SUPER_USER = EnumMember("super_user", {"default": "admin"})
|
||||||
DEFAULT_USER = EnumMember("default_user")
|
DEFAULT_USER = EnumMember("default_user", {"default": "user"})
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class UserBase(BotEntity, table=False):
|
|||||||
lang: LanguageBase = Field(
|
lang: LanguageBase = Field(
|
||||||
description="User language",
|
description="User language",
|
||||||
sa_type=EnumType(LanguageBase),
|
sa_type=EnumType(LanguageBase),
|
||||||
default_factory=lambda: LanguageBase.EN,
|
default_factory=lambda: LanguageBase.DEFAULT,
|
||||||
)
|
)
|
||||||
|
|
||||||
is_active: bool = EntityField(description="User is active", default=True)
|
is_active: bool = EntityField(description="User is active", default=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user