|
36 | 36 |
|
37 | 37 |
|
38 | 38 | class Translator(abc.ABC): |
| 39 | + """Abstract Base Class for command translators. |
| 40 | +
|
| 41 | + This class allows you to implement logic to translate messages sent via the :meth:`.commands.Context.send_translated` |
| 42 | + method in commands or anywhere :class:`.commands.Context` is available. |
| 43 | +
|
| 44 | + You should pass your implemented class to the :meth:`.commands.translator` decorator on top of a :class:`~.commands.Command`. |
| 45 | +
|
| 46 | + .. important:: |
| 47 | +
|
| 48 | + You must implement every method of this ABC. |
| 49 | + """ |
| 50 | + |
39 | 51 | @abc.abstractmethod |
40 | | - def get_langcode(self, ctx: Context[Any], name: str) -> str | None: ... |
| 52 | + def get_langcode(self, ctx: Context[Any], name: str) -> str | None: |
| 53 | + """Method which is called when :meth:`.commands.Context.send_translated` is used on a :class:`.commands.Command` |
| 54 | + which has an associated Translator, to determine the ``langcode`` which should be passed to :meth:`.translate` or ``None`` |
| 55 | + if the content should not be translated. |
| 56 | +
|
| 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. |
| 59 | +
|
| 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. |
| 62 | +
|
| 63 | + Parameters |
| 64 | + ---------- |
| 65 | + ctx: commands.Context |
| 66 | + The context surrounding the command invocation. |
| 67 | + name: str |
| 68 | + The ``name`` or ``alias`` used to invoke the command. This does not include the prefix, however if you need to |
| 69 | + retrieve the prefix see: :attr:`.commands.Context.prefix`. |
| 70 | +
|
| 71 | + Returns |
| 72 | + ------- |
| 73 | + str |
| 74 | + The language code as a :class:`str` to pass to :meth:`.translate`. |
| 75 | +
|
| 76 | + Example |
| 77 | + ------- |
| 78 | +
|
| 79 | + .. code:: python3 |
| 80 | + # For example purposes only the "get_langcode" method is shown in this example... |
| 81 | + # The "translate" method must also be implemented... |
| 82 | +
|
| 83 | + class HelloTranslator(commands.Translator): |
| 84 | + def __init__(self) -> None: |
| 85 | + self.code_mapping = {"hello": "en", "bonjour": "fr"} |
| 86 | +
|
| 87 | + def get_langcode(self, ctx: commands.Context, name: str) -> str | None: |
| 88 | + # Return a default of "en". Could also be ``None`` to prevent `translate` being called and to |
| 89 | + # send the default message... |
| 90 | + return self.code_mapping.get(name, "en") |
| 91 | +
|
| 92 | +
|
| 93 | + @commands.command(name="hello", aliases=["bonjour"]) |
| 94 | + @commands.translator(HelloTranslator) |
| 95 | + async def hello_command(ctx: commands.Context) -> None: |
| 96 | + await ctx.send_translated("Hello!") |
| 97 | + """ |
41 | 98 |
|
42 | 99 | @abc.abstractmethod |
43 | | - async def translate(self, ctx: Context[Any], text: str, langcode: str) -> str: ... |
| 100 | + async def translate(self, ctx: Context[Any], text: str, langcode: str) -> str: |
| 101 | + """|coro| |
| 102 | +
|
| 103 | + Method used to translate the content passed to :meth:`.commands.Context.send_translated` with the language code returned from |
| 104 | + :meth:`.get_langcode`. If ``None`` is returned from :meth:`.get_langcode`, this method will not be called and the |
| 105 | + default content provided to :meth:`~.commands.Context.send_translated` will be sent instead. |
| 106 | +
|
| 107 | + You could use this method to call a local or external translation API, retrieve translations from a database or local |
| 108 | + mapping etc. |
| 109 | +
|
| 110 | + This method must return a :class:`str`. |
| 111 | +
|
| 112 | + Parameters |
| 113 | + ---------- |
| 114 | + ctx: commands.Context |
| 115 | + The context surrounding the command invocation. |
| 116 | + text: str |
| 117 | + The content passed to :meth:`~.commands.Context.send_translated` which should be translated. |
| 118 | + langcode: str |
| 119 | + The language code returned via :meth:`.get_langcode`, which can be used to determine the language the text should |
| 120 | + be translated to. |
| 121 | +
|
| 122 | + Returns |
| 123 | + ------- |
| 124 | + str |
| 125 | + The translated text which should be sent. This should not exceed ``500`` characters. |
| 126 | +
|
| 127 | + Example |
| 128 | + ------- |
| 129 | +
|
| 130 | + .. code:: python3 |
| 131 | + # For example purposes only the "translate" method is shown in this example... |
| 132 | + # The "get_langcode" method must also be implemented... |
| 133 | +
|
| 134 | + class HelloTranslator(commands.Translator): |
| 135 | +
|
| 136 | + async def translate(self, ctx: commands.Context, text: str, langcode: str) -> str: |
| 137 | + # Usually you would call an API, or retrieve from a database or dict or some other solution... |
| 138 | + # This is just for an example... |
| 139 | +
|
| 140 | + if langcode == "en": |
| 141 | + return text |
| 142 | +
|
| 143 | + elif langcode == "fr": |
| 144 | + return "Bonjour!" |
| 145 | +
|
| 146 | + @commands.command(name="hello", aliases=["bonjour"]) |
| 147 | + @commands.translator(HelloTranslator) |
| 148 | + async def hello_command(ctx: commands.Context) -> None: |
| 149 | + await ctx.send_translated("Hello!") |
| 150 | + """ |
0 commit comments