This is a open source fork of Py18n whose purpose is to make commands translations easier in discord.py bots.
To install the package to your Python installation, clone the repository locally then run the following command in the repository's directory
py setup.py install
You can now use the library!
This library is meant to be flexible when it comes to using i18n, by default it'll try to load from the folder locale
.
In this example, we'll create a file locale/en-US.json
which will have the following information.
{
"Hello": "Hello there, {{author}}",
"commands": {
"hello": {
"name": "hello",
"description": "A command to say hello",
"params": {
"name": "user",
"description": "User that you want to greet"
},
}
}
}
It's important in this step to install I18nTranslator
, this class can be inherited and adjusted as needed.
By default it has a basic setup which works in the following way.
from discord import Locale, Intents, Member
from discord.ext import commands
from discord.ext.i18n import I18nTranslator, _
class Translator(I18nTranslator):
fallback = Locale.american_english
async def get_locale(self, ctx: commands.Context) -> Locale:
preferences = {678374009045254198: "es-419"}
return preferences.get(ctx.author.id, self.fallback)
class Bot(commands.Bot):
async def setup_hook(self):
await self.tree.set_translator(Translator(bot))
bot = Bot("prefix", intents=Intents.default())
@bot.hybrid_command()
async def hello(ctx: commands.Context, user: Member = commands.Author):
await ctx.reply(_("Hello", author=user.mention))
If you encounter any problems, check out current issues or make a new issue.