Skip to main content

Auto-translating i18n for discord.py — just write keys, strings translate themselves

Project description

dpy-i18n

Auto translating i18n for discord.py. Write translation keys, and they are resolved automatically in messages, embeds, and component labels.

Installation

Python 3.13 or higher is required

pip install dpy-i18n

Example Usage

import os
import discord
from dotenv import load_dotenv
from discord_i18n import I18nBot


class Bot(I18nBot):
    def __init__(self):
        load_dotenv()
        super().__init__(
            command_prefix="!",
            intents=discord.Intents.default(),
            lang_dir="lang",
            fallback="en",
        )

    async def setup_hook(self):
        await self.load_extension("cogs.test")
        await super().setup_hook()  # registers translator + syncs tree


if __name__ == "__main__":
    Bot().run(os.getenv("TOKEN"))

I18nBot automatically:

  • loads translations from lang/
  • patches Discord send/edit paths for auto-translation
  • registers the slash-command translator

Usage in Cogs

import discord
from discord import app_commands
from discord_i18n import I18nCog


class MyCog(I18nCog):
    @app_commands.command(
        name=app_commands.locale_str("test"),
        description=app_commands.locale_str("test_description"),
    )
    async def test(self, interaction: discord.Interaction):
        # 1) Plain key as content (auto-translated)
        await interaction.response.send_message("embed.test.title")

        # 2) Key inside a string
        await interaction.followup.send("🎉 {embed.test.title}")

        # 3) Keys in Embed fields (auto-translated)
        embed = discord.Embed(
            title="embed.test.title",
            description="embed.test.description",
        )
        embed.set_footer(text="embed.test.footer")
        await interaction.followup.send(embed=embed)

        # 4) Keys in UI labels (auto-translated)
        view = discord.ui.View()
        view.add_item(discord.ui.Button(label="embed.test.title"))
        await interaction.followup.send(view=view)

        # 5) Manual translation via self.t()
        text_a = self.t(interaction, "embed.test.title")

        # 6) Manual translation via module-level t()
        text_b = t(interaction, "embed.test.description")
        await interaction.followup.send(f"{text_a} | {text_b}")

Variables

lang/en.yaml:

welcome:
  message: "Hello, {user}!"

Usage:

await interaction.response.send_message("welcome.message", user=interaction.user.mention)

Translation Files

lang/
  en.yaml           # UI / message / embed keys
  de.yaml
  commands/
    en.yaml         # slash command names + descriptions
    de.yaml

Example lang/en.yaml:

embed:
  test:
    title: "Test Embed"
    description: "This is an English test embed."
    footer: "English"

Example lang/commands/en.yaml:

test: "test"
test_description: "Shows a test embed."

Important Note for Slash Commands

For command names/descriptions to localize correctly:

  • use app_commands.locale_str("...") in the decorator
  • keep keys in lang/commands/<locale>.yaml
  • sync commands after changes (await bot.tree.sync())

API

Symbol Description
I18nBot(**kwargs) Bot subclass. Supports lang_dir= and fallback=.
I18nCog Optional cog base class with self.t(...).
t(interaction_or_locale, key, **vars) Translate a key manually.
I18n(lang_dir, fallback) Core translation loader and resolver.
I18nTranslator Translator for slash command metadata.
apply_patches() Apply monkey-patches manually (done by I18nBot).

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

dpy_i18n-0.0.4.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

dpy_i18n-0.0.4-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file dpy_i18n-0.0.4.tar.gz.

File metadata

  • Download URL: dpy_i18n-0.0.4.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for dpy_i18n-0.0.4.tar.gz
Algorithm Hash digest
SHA256 41b5130ce9a4d349dc7f758e46c2b5bcb3bcdcbc86f2df06b4107b7a165918f1
MD5 de5f438f77240e05a671809fe2b1159a
BLAKE2b-256 aa7fb3308cfc8f560ba7d17666f8a7a167cf327fb6b35d389d615d8d6c049c85

See more details on using hashes here.

File details

Details for the file dpy_i18n-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: dpy_i18n-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for dpy_i18n-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b2857aa4d3aed6eeefef77ffb967565ba4ba7def1571267c0b56fca3e61a0d75
MD5 fb15196fef63135d92ec1183fa705e3a
BLAKE2b-256 3895f364422a4355ba4b16e8a8f3683122be5c04a7d94216625673cef4ea47cb

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