A extension supporting fully automated i18j translations for bot interface.
Project description
Auto Translation Extension for PyCord
Key Features
- Automatic text translations for messages, embeds etc..
- Zero code change necessary
- Fully customizable
- Forward Compatible
The extension is able to automatically translate all specified objects here into any registered language depending on the preferences of the channel or guild that the object is getting sent to. For instance, if a channel has the preference for Spanish, any messages, embeds etc.. that are being sent to the channel will be automatically translated into Spanish before it is sent.
Translations will carry over markdown!
+ I've set the language to `English` 🏴!
+ He establecido el idioma en `Spanish` 🇪🇸!
+ ငါဘာသာစကားကိုထားတယ် `မြန်မာ (ဗမာ)` 🇲🇲!
GoogleTranslated string in different languages with formatting maintained
Check out the FAQ for more information. This extension is relatively new, therefore please report any bugs at issues.
Fields Covered by Automatic Translation
Messages
Interaction Messages
Embeds
Buttons
Selects
Modals
(beta)Webhooks
(beta)
Installing
This is an extension for PyCord
. It is recommended that there exists an installation of py-cord>=2.0.0
.
To install this extension, run the following command:
# Linux/macOS
python3 -m pip install -U discord-ext-i18n
# Windows
py -3 -m pip install -U discord-ext-i18n
Quick Example
Required Steps:
- Define a language getter function decorated with the
Detector.language_getter
decorator (this getter is called with an ID of guilds / channels to see if it has a language preference) - Instantiate a
discord.ext.i18n.Agent
class to configure and inject code - Create a command for users to set preferences
from typing import Optional
from discord.ext import commands
from discord import Intents
from discord.ext.i18n import Agent, Language, Detector
intents = Intents.default()
# not necessary for the extension, enabled for msg commands
intents.messages = True
intents.message_content = True
bot = commands.Bot(
command_prefix="!",
intents=intents,
)
bot.preferences = {}
bot.agent = Agent(translate_all=True) # This must be instantiated at least and only once
@Detector.lang_getter
async def get_lang(id) -> Optional[Language]:
"""
This decorated function will be called internally to get Language
preferences.
"""
return bot.preferences.get(id, None)
@bot.command(name="lang")
async def set_lang(ctx, lang_code):
lang = Language.from_code(lang_code)
if lang is None:
return await ctx.reply("Bad language code!")
else:
# Set a language preference to the current channel.
bot.preferences[ctx.channel.id] = lang
await ctx.reply(f"I've set the language to `{lang.name.title()}` {lang.emoji}!")
@bot.command(name="hi")
async def greet(ctx):
# This will be translated before sent if necessary
await ctx.reply("Hey!!")
bot.run("TOKEN")
Features Extended & FAQ
- How do we tell the extension to translate x?
- How does the extension work?
- What does it use to translate the string?
- When are strings not translated?
How do we tell the extension to translate x?
By default, the extension will translate all messages and ignore others. If you want
to translate other objects such as buttons, embeds and so on, you will have
to explicitly specify them as a parameter when instantiating the Agent
class or modify the
translate_x
flag from the class. See detailed example here
Apart from that, you can call the usual methods like Messegable.send
,
ApplicationContext.respond
with your texts and the translation will be
handled in the backend. Absolutely
no code change is necessary when calling these high-level methods.
How does the extension work?
When you call high-level methods e.g. Messegable.send
the extension intercepts the text and the destination it's being sent to. It resolves
whether if this text requires translation by calling the language getter with the
ID of its destination. If it has one, it will append
the language code into an appropriate field and this appendage is later extracted
before text gets sent to the discord API where tokenization, translation, caching
and other book-keeping occurs if necessary.
What does it use to translate the string?
By default, the extension translates your strings using the Google Translator library. You can override this with your own translator like in the example here.
When are strings not translated?
Strings are not translated in cases where either if the text are already detected to be in the language that it should be translated into, or the destination no preference.
TODO / KNOWN ISSUES
[-] Defer interaction responses only if translation doesn't exist in cache
[-] Resolve the issue with not being able to translate in time for Modals
and they can't be deferred....
[-] Support translation in Webhooks
[-] More tests
Contributions are absolutely welcome, just create a pull-request and I'll merge them if reasonable.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.