[V3 i18n] Internationalise help for commands and cogs #1143
No reviewers
Labels
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
Mrcoolgamer11/Red-DiscordBot!1143
Loading…
Reference in a new issue
No description provided.
Delete branch "V3/internationalise_help"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Type
This PR is an alternative to #1121.
Breaking changes
redbot.core.RedContexthas been renamed and relocated toredbot.core.commands.Context.redbot.core.i18n.CogI18nhas been renamed toredbot.core.i18n.Translator.Description of the changes
redbot.core.contextmodule has been renamed toredbot.core.commandsand made into a package which acts as a substitute fordiscord.ext.commands.CommandandGroupclasses fromdiscord.ext.commandsare subclassed inredbot.core.commandsto change the behaviour of thehelpattribute. To instantiate commands/groups with these classes, thecommand()andgroup()decorators have also been provided in this module.CogI18nclass atredbot.core.i18nhas been renamed toTranslator, to avoid confusion with a new decorator, and because it doesn't just apply to cogs.cog_i18n(), located atredbot.core.i18n. It takes one argument - theTranslatorobject for that cog.Example Usage
There are some cases where both theUpdate:redbot.core.commandsanddiscord.ext.commandsmodules will want to be imported. For this, you should import at least one of the modules as an alias, e.g.redbot.core.commandsnow contains references to all non-modifieddiscord.ext.commandsattributes as well.All existing core cogs and modules have been updated to include this change.
I should also mention that
pygettext -Dcurrently has a bug when it comes to extracting docstrings, which makes internationalising help a bit harder. For the CogManagerUI I extracted the strings manually, however I'll look for a patch/alternative to pygettext in the near future to remedy this (Update: python/cpython#4745 has been merged and will be backported to the next release of 3.6 and 3.7)I'm actually going to make a few changes to this PR. In retrospect, perhaps naming the
redbot.core.commandswasn't the best idea, sincediscord.ext.commandswould need to be imported as an alias as outlined in the PR description.Some changes have been made here, and my decision to rename
redbot.core.commandshas somewhat changed; instead, I've made the module into a package, and it can now be imported entirely in place ofdiscord.ext.commands.Alright, so obviously this PR is a pretty massive change. Some people, naturally, won't like it due to the way it mocks up the discord.py commands extension. So, I'm going to lay out some reasons why it's a positive and necessary change, since that seems to be missing from this PR conversation.
Abstract
Changing locale at runtime is not a problem for strings passed into their
_()function, as long as said function is called every time that string is needed. However, due to the discord.py convention of having command and cog help being located in docstrings (a convention which should not be broken), translating these strings at runtime becomes a more complex issue.In order to solve the problem with commands, we need to have a better understanding of how discord.py manages command help after the command is instantiated, and how the help formatter retrieves it. The default
discord.ext.commands.Commandobject has just one relevant attribute here:help, which is simply assigned the value of thehelpkwarg if it exists, else it is assigned a cleaned up original async function's docstring. The help formatter then simply uses thishelpattribute in the embed which is sent to the user.From here, I believe we have two options: translate the command help from the help formatter and leave the
Commandobject as is, or subclass theCommandobject so that the translation occurs internally.Help formatter method
Doing translation within the help formatter would mean very minimal changes would need to be made to cogs. Perhaps an extra import and an extra decorator would be all it takes to make command help translatable at runtime. However, the help formatter itself would need to do some funky things to be able to do the translation. The original docstring of the callback would need to be accessed and the
helpattribute of theCommandwould be ignored. The help formatter would also need to find the_()function for this cog. This sort of operation, in my opinion, should be done internally by theCommandobject.. Not only this, but it complicates the writing of custom help formatters for Red. Also, should the help formatter have the responsibility of translation? It is a formatter after all.Subclassing method
This method is the one implemented in this PR. In most cases, updating a cog to command help translatable would be as simple as changing the line
from discord.ext import commandstofrom redbot.core import commands, and adding a single decorator to the cog class. Thehelpattribute has become a@property, and this is where the translation is done.However I didn't just see this method as a solution, but as an opportunity. Structuring the imports and core
commandspackage like this makes future subclassing and overriding ofdiscord.ext.commandsattributes more than trivial. The class can simply be added as an attribute toredbot.core.commands, and existing cogs needn't even be modified to be using the new object.Overriding
discord.ext.commandsSo, as this PR states, the new
redbot.core.commandsmodule acts as a complete substitute for importingdiscord.ext.commands. Theredbot.core.commandsmodule simply imports everything attached todiscord.ext.commands, then overrides any imports which we have subclassed. I think this is what's really making people apprehensive about this PR, however it would be nice to get some more specific thoughts. :)