According to the users' language setting, how can I make the application command's name have different names
For example, if one user sets his language of discord as English, the user can see the name of the application command in English. For another one, if another user sets his discord language as French, then the users can see the name of the identical application command in French.
To do this, I tried to utilize app_commands.locale_str
but it didn't work. I think that it's because the **kwarg
are passed and stored at the parameter extras
so it does not impact any translation. The following code says the code example that I tried.
@bot.tree.command(name=app_commands.locale_str(
"hello",
fr = 'bonjour',
),
guild=guild)
@app_commands.default_permissions(manage_channels = True)
async def hello(interaction: Interaction) -> None:
await interaction.response.send_message(content='hello')
Please could anyone suggest methods to implement the command that I explained above? Or please let me know how to implement this if anyone know this method.
You're in the right path, you need to also create your own Translator
class and handle all the translations there, example:
import discord
from discord import app_commands
@bot.tree.command(
name=app_commands.locale_str("hello"), guild=guild
)
@app_commands.default_permissions(manage_channels = True)
async def hello(interaction: Interaction) -> None:
await interaction.response.send_message(content='hello')
class MyTranslator(app_commands.Translator):
async def translate(
self,
string: app_commands.locale_str,
locale: discord.Locale,
context: app_commands.TranslationContext
) -> str | None:
if context.name == "hello": # check command name
if locale is discord.Locale.french: # check locale
return "bonjour"
return None
# set up your translator, e.g in setup_hook
async def setup_hook():
await bot.tree.set_translator(MyTranslator())
bot.setup_hook = setup_hook
Also, remember to sync your commands so the translation works.
Reference: