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.0.2.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.2-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: distrans-1.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 4bf633a1df555fe03316409cbb80cc7bd724a75cd5fba8860ba1b2935047c100
MD5 29fb3fd1547fa400a50a0d7a452bd0a6
BLAKE2b-256 47f7d7fc4068386954e7516925c83901c3cfb7966023899b99f00611bb8970a8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: distrans-1.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b5126e37a3d00bf0184317b78f31180271733d4803989dd568cfb385ee6f889e
MD5 a83cd2b61bd817eb5e6d28493d73586a
BLAKE2b-256 c75e0beef40fadca29de018231bd6f38aecb5d5f97e7899a6eeb13f1ac5c807a

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