2525from __future__ import annotations
2626
2727import abc
28- from typing import TYPE_CHECKING , Any
28+ from typing import TYPE_CHECKING , Any , Generic , TypeVar
2929
3030
3131if TYPE_CHECKING :
3232 from .context import Context
3333
3434
35+ T = TypeVar ("T" )
36+
37+
3538__all__ = ("Translator" ,)
3639
3740
38- class Translator (abc .ABC ):
41+ class Translator (Generic [ T ], abc .ABC ):
3942 """Abstract Base Class for command translators.
4043
4144 This class allows you to implement logic to translate messages sent via the :meth:`.commands.Context.send_translated`
@@ -49,16 +52,16 @@ class Translator(abc.ABC):
4952 """
5053
5154 @abc .abstractmethod
52- def get_langcode (self , ctx : Context [Any ], name : str ) -> str | None :
55+ def get_langcode (self , ctx : Context [Any ], name : str ) -> T | None :
5356 """Method which is called when :meth:`.commands.Context.send_translated` is used on a :class:`.commands.Command`
5457 which has an associated Translator, to determine the ``langcode`` which should be passed to :meth:`.translate` or ``None``
5558 if the content should not be translated.
5659
57- By default the ``name`` or ``alias`` used to invoke the command is passed alongside :class:`.commands.Context` to aid in
58- determining the ``langcode`` you should use.
60+ By default the lowercase ``name`` or ``alias`` used to invoke the command is passed alongside :class:`.commands.Context`
61+ to aid in determining the ``langcode`` you should use.
5962
60- You can use any system for the language codes, however they must be a :class:`str`. We also recommend using a
61- recognized system such as the ``ISO 639`` language code format .
63+ You can use any format or type for the language codes. We recommend using a recognized system such as the ``ISO 639``
64+ language code format as a :class:`str` .
6265
6366 Parameters
6467 ----------
@@ -70,8 +73,10 @@ def get_langcode(self, ctx: Context[Any], name: str) -> str | None:
7073
7174 Returns
7275 -------
73- str
74- The language code as a :class:`str` to pass to :meth:`.translate`.
76+ Any
77+ The language code to pass to :meth:`.translate`.
78+ None
79+ No translation attempt should be made with :meth:`.translate`.
7580
7681 Example
7782 -------
@@ -81,7 +86,7 @@ def get_langcode(self, ctx: Context[Any], name: str) -> str | None:
8186 # For example purposes only the "get_langcode" method is shown in this example...
8287 # The "translate" method must also be implemented...
8388
84- class HelloTranslator(commands.Translator):
89+ class HelloTranslator(commands.Translator[str] ):
8590 def __init__(self) -> None:
8691 self.code_mapping = {"hello": "en", "bonjour": "fr"}
8792
@@ -98,7 +103,7 @@ async def hello_command(ctx: commands.Context) -> None:
98103 """
99104
100105 @abc .abstractmethod
101- async def translate (self , ctx : Context [Any ], text : str , langcode : str ) -> str :
106+ async def translate (self , ctx : Context [Any ], text : str , langcode : T ) -> str :
102107 """|coro|
103108
104109 Method used to translate the content passed to :meth:`.commands.Context.send_translated` with the language code returned from
@@ -116,7 +121,7 @@ async def translate(self, ctx: Context[Any], text: str, langcode: str) -> str:
116121 The context surrounding the command invocation.
117122 text: str
118123 The content passed to :meth:`~.commands.Context.send_translated` which should be translated.
119- langcode: str
124+ langcode: Any
120125 The language code returned via :meth:`.get_langcode`, which can be used to determine the language the text should
121126 be translated to.
122127
@@ -133,7 +138,7 @@ async def translate(self, ctx: Context[Any], text: str, langcode: str) -> str:
133138 # For example purposes only the "translate" method is shown in this example...
134139 # The "get_langcode" method must also be implemented...
135140
136- class HelloTranslator(commands.Translator):
141+ class HelloTranslator(commands.Translator[str] ):
137142
138143 async def translate(self, ctx: commands.Context, text: str, langcode: str) -> str:
139144 # Usually you would call an API, or retrieve from a database or dict or some other solution...
0 commit comments