lithum-labs / Lithum-v2

GNU General Public License v3.0
0 stars 0 forks source link

多言語化 #6

Closed sonyakun closed 6 months ago

sonyakun commented 7 months ago

https://github.com/Rapptz/discord.py/discussions/9160 参考にすればいけそう

sonyakun commented 7 months ago

by R.Danny#6348

class MyCustomTranslator(app_commands.Translator):
  async def load(self):
    # this gets called when the translator first gets loaded!
  async def unload(self):
    # in case you need to switch translators, this gets called when being removed
  async def translate(self, string: app_commands.locale_str, locale: discord.Locale, context: app_commands.TranslationContext) -> Optional[str]:
    """
    `locale_str` is the string that is requesting to be translated
    `locale` is the target language to translate to
    `context` is the origin of this string, eg TranslationContext.command_name, etc
    This function must return a string (that's been translated), or `None` to signal no available translation available, and will default to the original.
    """
    message_str = string.message
    return your_translate(message_str)
# IMPORTANT! put this in your `setup_hook` function

await bot.tree.set_translator(MyCustomTranslator())
# Optional, but helps save a lot of typing
from discord.app_commands import locale_str as _T
@app_commands.command(name=_T("bonk"))
@app_commands.describe(user=_T("The user to bonk."))
async def bonk(interaction: discord.Interaction, user: discord.User):
  await interaction.response.send_message(f":hammer: {user.mention}")
sonyakun commented 7 months ago

うまく動かない...

sonyakun commented 6 months ago

一旦これは中止