Skip to main content

A simple translation library for discord bots

Project description

distrans

A simple package to translate your discord bot to different languages

Key Features

  • Fast work and async support.
  • The only requirement is disnake.
  • next-translate-like file structure and syntax.

Installing

Python 3.8 or higher is required.

# Linux/macOS
python3 -m pip install -U distrans

# Windows
py -3 -m pip install -U distrans

Quick Example

Inline translation example

from distrans import TranslationInteractionBot 

bot = TranslationInteractionBot(
    directory="locales",
    languages=["en", "fr", "es", "uk"]
)

@bot.slash_command()
async def ping(inter, language: str):
    await inter.send(bot.get(
    "common:greeting", 
    code=language, 
    values={"name": inter.user.name}
    )
  )


def main():
    bot.run("TOKEN")

if __name__ == "__main__":
    main()

Using async/await

translated = await bot.t(
    "common:greeting", 
    code=language, 
    values={"name": inter.user.name}
    )

Advanced usage

from distrans import TranslationInteractionBot

class MyTranslationInteractionBot(TranslationInteractionBot):
   
    def get_language(**kwargs):
        # kwargs - it's the same kwargs that you're passing to the
        # get() or t() method
        # like:
        # await bot.t("common:greeting", values={"name": "John"}, 
        #inter=inter)
        # you will get an inter object in kwargs
        # Your code here
        return "en" # you can return language code based on current context
        # (guild, user, etc.)

locales/en/common.json

{
  "greeting": "Hello, $name!"
}

locales/fr/common.json

{
  "greeting": "Bonjour, $name!"
}

locales/es/common.json

{
  "greeting": "Hola, $name!"
}

locales/uk/common.json

{
  "greeting": "Привіт, $name!"
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

distrans-1.1.1.tar.gz (5.8 kB view hashes)

Uploaded source

Built Distribution

distrans-1.1.1-py3-none-any.whl (6.4 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page