0

我正在编写一个 Discord 机器人。在那里你定义你的命令,比如

@commands.command()
async def hello(self, ctx):
    await ctx.send("Hello world!")

这将创建一个命令hello,该命令将发送一条消息。

现在我想为它添加翻译。在我的数据库中,我通过公会 ID 存储每个公会的语言。公会 ID 传入ctx.guild.id。我想要一个翻译功能来处理这个过程。而且我不想每次需要翻译时都传递公会ID。我已经看到了一些使用ContextVars. 但我不知道它是如何工作的。

有人对如何处理它有建议吗?IdleRPG 也集成了这一点,但非常复杂:https ://github.com/Gelbpunkt/IdleRPG/blob/v4/utils/i18n.py

实际的翻译功能似乎不是问题。最后它应该看起来像这样。

def _(self, text):
    # Do gettext stuff here (This is the part I know about)
    # Get the context without the need to pass it to the function (But how?)
    return translation

@commands.command()
async def hello(self, ctx):
    await ctx.send(_("Hello world")) # Auto pass ctx to _(), but how?
4

0 回答 0