Refactor TranslationContext to be more useful

The previous enum was good at accomplishing dynamic key generation for
a few cases, but it fell short in others:

1. It could not discern group names and command names
2. It could not give you more contextual data such as the full object
   currently being translated.

On top of that, the context being a required parameter for
Translator.translate meant that it wouldn't be possible to re-use the
translator for other use cases outside of the rigid ones defined in the
library.

To alleviate these concerns, new enum attributes were added along with
a richer type for obtaining even more context.
This commit is contained in:
Rapptz
2022-08-09 10:36:30 -04:00
parent d826f4f3a8
commit c32567ea81
5 changed files with 136 additions and 34 deletions

View File

@ -626,10 +626,18 @@ locale_str
TranslationContext
+++++++++++++++++++
.. class:: TranslationContext
.. attributetable:: discord.app_commands.TranslationContext
.. autoclass:: discord.app_commands.TranslationContext
:members:
TranslationContextLocation
+++++++++++++++++++++++++++
.. class:: TranslationContextLocation
:module: discord.app_commands
An enum representing the context that the translation occurs in when requested for translation.
An enum representing the location context that the translation occurs in when requested for translation.
.. versionadded:: 2.0
@ -639,6 +647,13 @@ TranslationContext
.. attribute:: command_description
The translation involved a command description.
.. attribute:: group_name
The translation involved a group name.
.. attribute:: group_description
The translation involved a group description.
.. attribute:: parameter_name
The translation involved a parameter name.
@ -648,7 +663,10 @@ TranslationContext
.. attribute:: choice_name
The translation involved a choice name.
.. attribute:: other
The translation involved something else entirely. This is useful for running
:meth:`Translator.translate` for custom usage.
Exceptions
~~~~~~~~~~~