Translation API

This page assumes you’ve completed Creating a Simple Hello World Module. If not, check the previous pages.

If you want your modules to be translate-able, you can use the Translation API.

Translation Files

Translation files use the HOCON syntax. They don’t have any standard directory, however it’s recommended to keep them inside the module directory somewhere since they will most likely be non-editable.

Registering Languages

Before doing anything, you’ll need to register your language files using register_language:

async def on_ready(self):
    module_dir = os.path.dirname(os.path.abspath(__file__))

    # self.lang is the Translation instance of all modules.
    self.lang.register_language(
        "en",
        Configuration(os.path.join(module_dir, "en.conf")),
        default=True
    )

    self.lang.register_language(
        "tr",
        Configuration(os.path.join(module_dir, "tr.conf")),
    )

Getting Translated Text

To get translated text, we use get:

async def run(self, message: discord.Message, **kwargs):
    name = kwargs.get(
        "name",
        self.module.lang.get(message.server.id, "default_name")
    )

    # text == "Hello, {name}"
    # The last dict contains format variables.
    text = self.module.lang.get(message.server.id, "text", {"name": name})

    await self.client.send_message(message.channel, text)

And that’s about it for translation. The same module (with some comments) can be found on our examples repository