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

``` 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.0.1.tar.gz (5.2 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.0.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for distrans-1.0.1.tar.gz
Algorithm Hash digest
SHA256 7399c6174fd2999a2c63e9965d4a32baf27abde819b467905f6d0e16c10dbbd8
MD5 9e6e9dc6163844e5043845b96f6c6254
BLAKE2b-256 0f59ad70ed3e01014d5885136065054cdff7844fcf14b5faaeee2ed8a50c8abe

See more details on using hashes here.

File details

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

File metadata

  • Download URL: distrans-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.1 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.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fb5d16832365f636ebb8cae0696ea00e0e4e71eb76e9d738f0d1ebcc87507872
MD5 ffb5dc2a304c1ed514496a4a5450d16d
BLAKE2b-256 d12fd7f8775b00ac5594609939c6c8c5daf7e559d56e1a488ae3bde58b5f61f4

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