Skip to content

Commit

Permalink
✨ 支持 i18n #410
Browse files Browse the repository at this point in the history
  • Loading branch information
shoucandanghehe committed Oct 22, 2024
1 parent 47c83be commit 3b98760
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 15 deletions.
3 changes: 2 additions & 1 deletion nonebot_plugin_tetris_stats/games/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from nonebot_plugin_alconna import AlcMatches, Alconna, At, CommandMeta, on_alconna

from .. import ns
from ..i18n.model import Lang
from ..utils.exception import MessageFormatError, NeedCatchError

command: Alconna = Alconna(
Expand All @@ -30,7 +31,7 @@ def add_block_handlers(handler: Callable[[T_Handler], T_Handler]) -> None:
@handler
async def _(bot: Bot, matcher: Matcher, target: At):
if isinstance(target, At) and target.target == bot.self_id:
await matcher.finish('不能查询bot的信息')
await matcher.finish(Lang.interaction.wrong.query_bot())


from . import tetrio, top, tos # noqa: F401, E402
Expand Down
1 change: 0 additions & 1 deletion nonebot_plugin_tetris_stats/games/constant.py

This file was deleted.

8 changes: 5 additions & 3 deletions nonebot_plugin_tetris_stats/games/tetrio/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from yarl import URL

from ...db import query_bind_info, trigger
from ...i18n import Lang
from ...utils.host import HostPage, get_self_netloc
from ...utils.metrics import get_metrics
from ...utils.render import render
Expand All @@ -36,7 +37,6 @@
from ...utils.screenshot import screenshot
from ...utils.typing import Me
from .. import add_block_handlers, alc
from ..constant import CANT_VERIFY_MESSAGE
from . import command, get_player
from .api import Player
from .api.schemas.summaries.league import NeverPlayedData, NeverRatedData
Expand Down Expand Up @@ -120,9 +120,11 @@ async def _( # noqa: PLR0913
)
if bind is None:
await matcher.finish('未查询到绑定信息')
message = UniMessage(CANT_VERIFY_MESSAGE)
player = Player(user_id=bind.game_account, trust=True)
await (message + UniMessage.image(raw=await make_query_image_v2(player))).finish()
await (
UniMessage.i18n(Lang.interaction.warning.unverified)
+ UniMessage.image(raw=await make_query_image_v2(player))
).finish()


@alc.assign('TETRIO.query')
Expand Down
7 changes: 4 additions & 3 deletions nonebot_plugin_tetris_stats/games/tetrio/record/blitz.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from yarl import URL

from ....db import query_bind_info, trigger
from ....i18n import Lang
from ....utils.exception import RecordNotFoundError
from ....utils.host import HostPage, get_self_netloc
from ....utils.metrics import get_metrics
Expand All @@ -22,7 +23,6 @@
from ....utils.render.schemas.tetrio.record.blitz import Record, Statistic
from ....utils.screenshot import screenshot
from ....utils.typing import Me
from ...constant import CANT_VERIFY_MESSAGE
from .. import alc
from ..api.player import Player
from ..constant import GAME_TYPE
Expand Down Expand Up @@ -60,9 +60,10 @@ async def _(
)
if bind is None:
await matcher.finish('未查询到绑定信息')
message = UniMessage(CANT_VERIFY_MESSAGE)
player = Player(user_id=bind.game_account, trust=True)
await (message + UniMessage.image(raw=await make_blitz_image(player))).finish()
await (
UniMessage.i18n(Lang.interaction.warning.unverified) + UniMessage.image(raw=await make_blitz_image(player))
).finish()


@alc.assign('TETRIO.record.blitz')
Expand Down
7 changes: 4 additions & 3 deletions nonebot_plugin_tetris_stats/games/tetrio/record/sprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from yarl import URL

from ....db import query_bind_info, trigger
from ....i18n import Lang
from ....utils.exception import RecordNotFoundError
from ....utils.host import HostPage, get_self_netloc
from ....utils.metrics import get_metrics
Expand All @@ -22,7 +23,6 @@
from ....utils.render.schemas.tetrio.record.sprint import Record
from ....utils.screenshot import screenshot
from ....utils.typing import Me
from ...constant import CANT_VERIFY_MESSAGE
from .. import alc
from ..api.player import Player
from ..constant import GAME_TYPE
Expand Down Expand Up @@ -60,9 +60,10 @@ async def _(
)
if bind is None:
await matcher.finish('未查询到绑定信息')
message = UniMessage(CANT_VERIFY_MESSAGE)
player = Player(user_id=bind.game_account, trust=True)
await (message + UniMessage.image(raw=await make_sprint_image(player))).finish()
await (
UniMessage.i18n(Lang.interaction.warning.unverified) + UniMessage.image(raw=await make_sprint_image(player))
).finish()


@alc.assign('TETRIO.record.sprint')
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_tetris_stats/games/top/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from nonebot_plugin_user import get_user

from ...db import query_bind_info, trigger
from ...i18n import Lang
from ...utils.exception import FallbackError
from ...utils.host import HostPage, get_self_netloc
from ...utils.metrics import TetrisMetricsBasicWithLPM, get_metrics
Expand All @@ -18,7 +19,6 @@
from ...utils.render.schemas.top_info import Info
from ...utils.screenshot import screenshot
from ...utils.typing import Me
from ..constant import CANT_VERIFY_MESSAGE
from . import alc
from .api import Player
from .api.schemas.user_profile import Data, UserProfile
Expand All @@ -44,7 +44,7 @@ async def _(event: Event, matcher: Matcher, target: At | Me, event_session: Even
if bind is None:
await matcher.finish('未查询到绑定信息')
await (
UniMessage(CANT_VERIFY_MESSAGE)
UniMessage.i18n(Lang.interaction.warning.unverified)
+ await make_query_result(await Player(user_name=bind.game_account, trust=True).get_profile())
).finish()

Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_tetris_stats/games/tos/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from nonebot_plugin_userinfo import EventUserInfo, UserInfo

from ...db import query_bind_info, trigger
from ...i18n import Lang
from ...utils.exception import RequestError
from ...utils.host import HostPage, get_self_netloc
from ...utils.image import get_avatar
Expand All @@ -24,7 +25,6 @@
from ...utils.render.schemas.tos_info import Info, Multiplayer, Radar
from ...utils.screenshot import screenshot
from ...utils.typing import Me, Number
from ..constant import CANT_VERIFY_MESSAGE
from . import alc
from .api import Player
from .api.schemas.user_info import UserInfoSuccess
Expand Down Expand Up @@ -124,7 +124,7 @@ async def _(
)
if bind is None:
await matcher.finish('未查询到绑定信息')
message = CANT_VERIFY_MESSAGE
message = UniMessage.i18n(Lang.interaction.warning.unverified)
player = Player(teaid=bind.game_account, trust=True)
user_info, game_data = await gather(player.get_info(), get_game_data(player))
if game_data is not None:
Expand Down
5 changes: 5 additions & 0 deletions nonebot_plugin_tetris_stats/i18n/.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"default": "zh-CN",
"frozen": [],
"require": []
}
72 changes: 72 additions & 0 deletions nonebot_plugin_tetris_stats/i18n/.lang.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"title": "Lang Schema",
"description": "Schema for lang file",
"type": "object",
"properties": {
"interaction": {
"title": "Interaction",
"description": "Scope 'interaction' of lang item",
"type": "object",
"additionalProperties": false,
"properties": {
"wrong": {
"title": "Wrong",
"description": "Scope 'wrong' of lang item",
"type": "object",
"additionalProperties": false,
"properties": {
"query_bot": {
"title": "query_bot",
"description": "value of lang item type 'query_bot'",
"type": "string"
}
}
},
"warning": {
"title": "Warning",
"description": "Scope 'warning' of lang item",
"type": "object",
"additionalProperties": false,
"properties": {
"unverified": {
"title": "unverified",
"description": "value of lang item type 'unverified'",
"type": "string"
}
}
}
}
},
"error": {
"title": "Error",
"description": "Scope 'error' of lang item",
"type": "object",
"additionalProperties": false,
"properties": {
"MessageFormatError": {
"title": "Messageformaterror",
"description": "Scope 'MessageFormatError' of lang item",
"type": "object",
"additionalProperties": false,
"properties": {
"TETR.IO": {
"title": "TETR.IO",
"description": "value of lang item type 'TETR.IO'",
"type": "string"
},
"TOS": {
"title": "TOS",
"description": "value of lang item type 'TOS'",
"type": "string"
},
"TOP": {
"title": "TOP",
"description": "value of lang item type 'TOP'",
"type": "string"
}
}
}
}
}
}
}
16 changes: 16 additions & 0 deletions nonebot_plugin_tetris_stats/i18n/.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": ".template.schema.json",
"scopes": [
{
"scope": "interaction",
"types": [
{ "subtype": "wrong", "types": ["query_bot"] },
{ "subtype": "warning", "types": ["unverified"] }
]
},
{
"scope": "error",
"types": [{ "subtype": "MessageFormatError", "types": ["TETR.IO", "TOS", "TOP"] }]
}
]
}
54 changes: 54 additions & 0 deletions nonebot_plugin_tetris_stats/i18n/.template.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"title": "Template",
"description": "Template for lang items to generate schema for lang files",
"type": "object",
"properties": {
"scopes": {
"title": "Scopes",
"description": "All scopes of lang items",
"type": "array",
"uniqueItems": true,
"items": {
"title": "Scope",
"description": "First level of all lang items",
"type": "object",
"properties": {
"scope": {
"type": "string",
"description": "Scope name"
},
"types": {
"type": "array",
"description": "All types of lang items",
"uniqueItems": true,
"items": {
"oneOf": [
{
"type": "string",
"description": "Value of lang item"
},
{
"type": "object",
"properties": {
"subtype": {
"type": "string",
"description": "Subtype name of lang item"
},
"types": {
"type": "array",
"description": "All subtypes of lang items",
"uniqueItems": true,
"items": {
"$ref": "#/properties/scopes/items/properties/types/items"
}
}
}
}
]
}
}
}
}
}
}
}
10 changes: 10 additions & 0 deletions nonebot_plugin_tetris_stats/i18n/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file is @generated by tarina.lang CLI tool
# It is not intended for manual editing.

from pathlib import Path

from tarina.lang import lang # type: ignore[import-untyped]

lang.load(Path(__file__).parent)

from .model import Lang # noqa: E402, F401
16 changes: 16 additions & 0 deletions nonebot_plugin_tetris_stats/i18n/en-US.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": ".lang.schema.json",
"interaction": {
"wrong": { "query_bot": "Can't query bot's information" },
"warning": {
"unverified": "* Because I can't verify account linking information, I can't guarantee the info I found is yourself/themself."
}
},
"error": {
"MessageFormatError": {
"TETR.IO": "Username/ID is invalid",
"TOS": "Username/ID is invalid",
"TOP": "Username is invalid"
}
}
}
32 changes: 32 additions & 0 deletions nonebot_plugin_tetris_stats/i18n/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This file is @generated by tarina.lang CLI tool
# It is not intended for manual editing.

from tarina.lang.model import LangItem, LangModel # type: ignore[import-untyped]


class InteractionWrong:
query_bot: LangItem = LangItem('interaction', 'wrong.query_bot')


class InteractionWarning:
unverified: LangItem = LangItem('interaction', 'warning.unverified')


class Interaction:
wrong = InteractionWrong
warning = InteractionWarning


class ErrorMessageformaterror:
TETR_IO: LangItem = LangItem('error', 'MessageFormatError.TETR.IO')
TOS: LangItem = LangItem('error', 'MessageFormatError.TOS')
TOP: LangItem = LangItem('error', 'MessageFormatError.TOP')


class Error:
MessageFormatError = ErrorMessageformaterror


class Lang(LangModel):
interaction = Interaction
error = Error
14 changes: 14 additions & 0 deletions nonebot_plugin_tetris_stats/i18n/zh-CN.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": ".lang.schema.json",
"interaction": {
"wrong": { "query_bot": "不能查询bot的信息" },
"warning": { "unverified": "* 由于无法验证绑定信息, 不能保证查询到的用户为本人" }
},
"error": {
"MessageFormatError": {
"TETR.IO": "用户名/ID不合法",
"TOS": "用户名/ID不合法",
"TOP": "用户名不合法"
}
}
}

0 comments on commit 3b98760

Please sign in to comment.