Skip to main content

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

Project description

discord-i18n

Auto-translating i18n for discord.py. Write translation keys — they get resolved automatically when you send messages, embeds, or buttons.

Installation

pip install -e .

Setup (main.py)

from discord_i18n import I18nBot

bot = I18nBot(command_prefix="!", intents=discord.Intents.default())
# That's it. I18nBot:
#  - loads lang/ automatically
#  - patches send_message / edit_message so keys are auto-translated
#  - registers the slash-command translator

Usage in Cogs

from discord_i18n import I18nCog, t

class MyCog(I18nCog):

    @app_commands.command(...)
    async def cmd(self, interaction):

        # 1. Plain key as content — auto-translated
        await interaction.response.send_message("embed.test.title")

        # 2. Key embedded in a string
        await interaction.response.send_message("🎉 {embed.test.title}")

        # 3. Key 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.response.send_message(embed=embed)

        # 4. Key in Button labels — auto-translated
        view = discord.ui.View()
        view.add_item(discord.ui.Button(label="embed.test.title"))
        await interaction.response.send_message(view=view)

        # 5. Manual via self.t() — when you need the string first
        text = self.t(interaction, "embed.test.title")

        # 6. Manual via module-level t() — usable anywhere
        text = t(interaction, "embed.test.title")

Variables

# lang/en.yaml
welcome:
  message: "Hello, {user}!"
await interaction.response.send_message("welcome.message", user=interaction.user.mention)

Directory layout

lang/
  en.yaml           ← UI / embed strings
  de.yaml
  commands/
    en.yaml         ← slash-command names & descriptions
    de.yaml

lang/en.yaml

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

lang/commands/en.yaml

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

API

Symbol Description
I18nBot(**kwargs) Bot subclass. Pass lang_dir= and fallback= to customise.
I18nCog Optional Cog base class. Adds self.t(interaction, key, **vars).
t(interaction, key, **vars) Translate a key manually. Works anywhere.
I18n(lang_dir, fallback) Core class — use directly if you don't want I18nBot.
apply_patches() Apply the monkey-patches manually (called by I18nBot automatically).

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.3.tar.gz (7.8 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.3-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dpy_i18n-0.0.3.tar.gz
  • Upload date:
  • Size: 7.8 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.3.tar.gz
Algorithm Hash digest
SHA256 5054ed1114146129d3c6daf21f47fc1a0c31dfbbeac0d69d0a1095d1e9537f97
MD5 ad38e61d6134c064e9b77eaffdbdcf6a
BLAKE2b-256 4dd1a39054a0978b8b99a68ed2c9b818763b6db2bd2c5b7360e2451a78d6b7d0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dpy_i18n-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 8.8 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 68f6eab3ae514668e0c47760981ceb881d8dfd373a7a50e8577ad9cd6594745e
MD5 6078716058844344c2cdffa47deb45f9
BLAKE2b-256 8c7633fcff2e3191335d9e5d421b7620dd473ead1762dba93cd65d8a5313afad

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