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.0.tar.gz (5.6 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.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: distrans-1.1.0.tar.gz
  • Upload date:
  • Size: 5.6 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.0.tar.gz
Algorithm Hash digest
SHA256 1b72fb79de87c80ccadae33a87c25571345787aa9d5a8ee63eb43558e4132fc2
MD5 6f92cb52a1aa33c2a85181a62d905f51
BLAKE2b-256 0324731c133f39505e6594ebb5f0e5c119f1c9e1e2c40141f2714babf2525472

See more details on using hashes here.

File details

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

File metadata

  • Download URL: distrans-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6297c203c91d956d527e1644131ba25a6dd7b8177e7bd98228d73a928b0df7f
MD5 065cc243ad6f6db59172628df6bfaa0b
BLAKE2b-256 e15b84fe72a0681f0125ac92d38bc7bec61c89e58bdec42b770b2a51c494383f

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