How to add localization to describe a command and its arguments in discord.py . I've seen that Juniper Bot can do this depending on the Discord language.
Here is my code:
@bot.tree.command(name='addcolorrole', description='Creates or updates a color role')
@app_commands.describe(color='π΅π²π
π°πΌπ±π², "pfp" or "random"')
async def addcolorrole(ctx, color: str):
This can be accomplished by creating a custom Translator, setting it as the translator for your bot tree and then using locale_str
for localizable strings:
import discord
from discord import app_commands
from discord.app_commands import locale_str
class MyTranslator(app_commands.Translator):
async def translate(self, string: locale_str, locale: discord.Locale, context: app_commands.TranslationContext):
# you can use a translation api, fetch it from a file or whatever else
# here we instead hardcode example translations
translations = {
'addcolorrole': {
'es-ES': 'agregarrolcolor',
},
'Creates or updates a color role': {
'es-ES': 'Crea o actualiza un rol de color',
},
'hex code, "pfp" or "random"': {
'es-ES': 'cΓ³digo hexadecimal, "pfp" o "random"',
},
}
return translations.get(string.message, {}).get(locale.value, string.message)
bot.tree.set_translator(MyTranslator())
@bot.tree.command(
name=locale_str('addcolorrole'),
description=locale_str('Creates or updates a color role')
)
@app_commands.describe(
color=locale_str('hex code, "pfp" or "random"')
)
async def addcolorrole(ctx, color: str):