Make command errors translatable (#3665)

* Make command errors translatable

* style

* moar style

* Update events.py

* Separate strings to make it better for translators
This commit is contained in:
jack1142 2020-03-24 00:35:32 +01:00 committed by GitHub
parent a88b2af4a9
commit 2cdf3c16ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,6 +14,7 @@ from colorama import Fore, Style, init
from pkg_resources import DistributionNotFound from pkg_resources import DistributionNotFound
from redbot.core.commands import RedHelpFormatter from redbot.core.commands import RedHelpFormatter
from redbot.core.i18n import Translator
from .. import __version__ as red_version, version_info as red_version_info, VersionInfo from .. import __version__ as red_version, version_info as red_version_info, VersionInfo
from . import commands from . import commands
from .config import get_latest_confs from .config import get_latest_confs
@ -32,6 +33,8 @@ ______ _ ______ _ _ ______ _
\_| \_\___|\__,_| |___/ |_|___/\___\___/|_| \__,_| \____/ \___/ \__| \_| \_\___|\__,_| |___/ |_|___/\___\___/|_| \__,_| \____/ \___/ \__|
""" """
_ = Translator(__name__, __file__)
def init_events(bot, cli_flags): def init_events(bot, cli_flags):
@bot.event @bot.event
@ -154,7 +157,9 @@ def init_events(bot, cli_flags):
if isinstance(error, commands.MissingRequiredArgument): if isinstance(error, commands.MissingRequiredArgument):
await ctx.send_help() await ctx.send_help()
elif isinstance(error, commands.ArgParserFailure): elif isinstance(error, commands.ArgParserFailure):
msg = f"`{error.user_input}` is not a valid value for `{error.cmd}`" msg = _("`{user_input}` is not a valid value for `{command}`").format(
user_input=error.user_input, command=error.cmd,
)
if error.custom_help_msg: if error.custom_help_msg:
msg += f"\n{error.custom_help_msg}" msg += f"\n{error.custom_help_msg}"
await ctx.send(msg) await ctx.send(msg)
@ -177,9 +182,9 @@ def init_events(bot, cli_flags):
exc_info=error.original, exc_info=error.original,
) )
message = "Error in command '{}'. Check your console or logs for details.".format( message = _(
ctx.command.qualified_name "Error in command '{command}'. Check your console or logs for details."
) ).format(command=ctx.command.qualified_name)
exception_log = "Exception in command '{}'\n" "".format(ctx.command.qualified_name) exception_log = "Exception in command '{}'\n" "".format(ctx.command.qualified_name)
exception_log += "".join( exception_log += "".join(
traceback.format_exception(type(error), error, error.__traceback__) traceback.format_exception(type(error), error, error.__traceback__)
@ -201,35 +206,35 @@ def init_events(bot, cli_flags):
await ctx.send(await format_fuzzy_results(ctx, fuzzy_commands, embed=False)) await ctx.send(await format_fuzzy_results(ctx, fuzzy_commands, embed=False))
elif isinstance(error, commands.BotMissingPermissions): elif isinstance(error, commands.BotMissingPermissions):
if bin(error.missing.value).count("1") == 1: # Only one perm missing if bin(error.missing.value).count("1") == 1: # Only one perm missing
plural = "" msg = _("I require the {permission} permission to execute that command.").format(
else: permission=format_perms_list(error.missing)
plural = "s"
await ctx.send(
"I require the {perms} permission{plural} to execute that command.".format(
perms=format_perms_list(error.missing), plural=plural
) )
) else:
msg = _("I require {permission_list} permissions to execute that command.").format(
permission_list=format_perms_list(error.missing)
)
await ctx.send(msg)
elif isinstance(error, commands.UserFeedbackCheckFailure): elif isinstance(error, commands.UserFeedbackCheckFailure):
if error.message: if error.message:
await ctx.send(error.message) await ctx.send(error.message)
elif isinstance(error, commands.NoPrivateMessage): elif isinstance(error, commands.NoPrivateMessage):
await ctx.send("That command is not available in DMs.") await ctx.send(_("That command is not available in DMs."))
elif isinstance(error, commands.PrivateMessageOnly): elif isinstance(error, commands.PrivateMessageOnly):
await ctx.send("That command is only available in DMs.") await ctx.send(_("That command is only available in DMs."))
elif isinstance(error, commands.CheckFailure): elif isinstance(error, commands.CheckFailure):
pass pass
elif isinstance(error, commands.CommandOnCooldown): elif isinstance(error, commands.CommandOnCooldown):
await ctx.send( if delay := humanize_timedelta(seconds=error.retry_after):
"This command is on cooldown. Try again in {}.".format( msg = _("This command is on cooldown. Try again in {delay}.").format(delay=delay)
humanize_timedelta(seconds=error.retry_after) or "1 second" else:
), msg = _("This command is on cooldown. Try again in 1 second.")
delete_after=error.retry_after, await ctx.send(msg, delete_after=error.retry_after)
)
elif isinstance(error, commands.MaxConcurrencyReached): elif isinstance(error, commands.MaxConcurrencyReached):
await ctx.send( await ctx.send(
"Too many people using this command. It can only be used {} time(s) per {} concurrently.".format( _(
error.number, error.per.name "Too many people using this command."
) " It can only be used {number} time(s) per {type} concurrently."
).format(number=error.number, type=error.per.name)
) )
else: else:
log.exception(type(error).__name__, exc_info=error) log.exception(type(error).__name__, exc_info=error)