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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file distrans-1.1.1.tar.gz.

File metadata

  • Download URL: distrans-1.1.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for distrans-1.1.1.tar.gz
Algorithm Hash digest
SHA256 3592512f07bfccc2e7813e2e2eb2eaca0f15b4488b1a45c97ecf1acb158f37b5
MD5 363836e33c43bb702e0abe625728bed7
BLAKE2b-256 b7889f8dabd7821906e2761b7f31bb3c423f98e4331ff2f3398810e61642cc0a

See more details on using hashes here.

File details

Details for the file distrans-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: distrans-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for distrans-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b26bad7986cc419b986ef07b128ef1f7c40201b6327088bae80e57c1bae935be
MD5 2587aa53079c69f19cb4a3c0a06ecb13
BLAKE2b-256 b8a36166df2520259fac3cfa04ea9c2ad7d47475bdfa67da6f3b4cd3b4856800

See more details on using hashes here.

Supported by

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