Skip to content

Conversation

@EvieePy
Copy link
Member

@EvieePy EvieePy commented Aug 5, 2025

Description

Adds an interface to translate content sent from Context in a Command.

The ABC commands.Translator can be implemented and added to a command by users to translate content sent via Context.send_translated.

Example:

class HelloTranslator(commands.Translator):
    def __init__(self) -> None:
        self.translations = {"fr": {"Hello World!": "Bonjour le monde!"}}

    def get_langcode(self, ctx: commands.Context, name: str) -> str | None:
        if name == "bonjour":
            return "fr"

    async def translate(self, ctx: commands.Context, text: str, langcode: str) -> str:
        # Ideally you store translations in another medium or call an API etc...
        translations = self.translations.get(langcode, {})
        translation = translations.get(text, text)
        
        return translation


@commands.command(aliases=["bonjour"])
@commands.translator(HelloTranslator)
async def hello(ctx: commands.Context) -> None:
    await ctx.send_translated("Hello World!")

Added:

  • commands.Translator
  • commands.translator
  • Context.send_translated
  • commands.TranslatorError
  • commands.Command.translator

Checklist

  • If code changes were made then they have been tested.
    • I have updated the documentation to reflect the changes.
    • I have updated the changelog with a quick recap of my changes.
  • This PR fixes an issue.
  • This PR adds something new (e.g. new method or parameters).
  • This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • This PR is not a code change (e.g. documentation, README, ...)
  • I have read and agree to the Developer Certificate of Origin for this contribution

@EvieePy EvieePy added this to the 3.1 milestone Aug 5, 2025
@EvieePy EvieePy marked this pull request as draft August 5, 2025 06:12
@EvieePy EvieePy marked this pull request as ready for review August 8, 2025 01:42
@EvieePy EvieePy merged commit 20fe33b into main Aug 10, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants