Skip to main content

A discord.py extension for command localization.

Project description

Github | PyPI


Setting up

Start by downloading the package via pip.

python3 -m pip install discord-localization

After that, you can just import it into your file as a discord.py extension.


Examples

from discord.ext import localization
import random

_ = localization.Localization("main.i18n.json", "en") # the first option is the relative path to the localization file. the second is the fallback/default language that is used if the actual language is not found.
dice = random.randint(1, 6)
language = input("What's your native language? / Was ist Deine Muttersprache? (en/de) >>> ")

print(_("dice_result", language, dice=dice))
{
   "en": {
       "dice_result": "Your dice rolled {dice}!"
   },
   "de": {
       "dice_result": "Dein Würfel hat eine {dice} gewürfelt!"
   }
}

Integrating it into your Discord bot

import discord
from discord.ext import commands, localization

bot = commands.Bot(command_prefix="!", intents=discord.Intents.all()) # hey guys, breaking the 4th wall here. please stop using Intents.default() just to set the message_content intent to True right after. Intents.all() does it for you.
_ = localization.Localization("main_localization.json", "en")

@bot.event
async def on_ready():
   print("Bot is ready!")

@bot.command()
async def ping(ctx):
   await ctx.reply(_("ping", ctx, latency=bot.latency * 1000))
{
   "en": {
       "ping": "Pong! {latency}ms"
   },
   "en-US": {
       "ping": "Pong! {latency}ms, but American! (because Discord makes a difference between en-US and en-UK locales - you can circumvent this by setting the default_locale)"
   },
   "en-UK": {
       "ping": "Ping is {latency}ms, innit, bruv? (i'm sorry)"
   },
   "fr": {
       "ping": "Bonjour! Ping is {latency}ms!"
   }
}

Explanation:

  • By setting the default locale to "en", we won't have any issues with unfinished localizations. This way, we are also not required to define localizations for both "en-US" and "en-UK" (since Discord's API differentiates them, however, usually, bots don't.)
  • We are passing a latency argument to the localization function (which is also available with Localization._! But calling the object is fine too.), which will be later used whenever we use {latency} in a localization.
  • We are defining an en locale in the JSON file, even though both Discord's API and discord.py doesn't have such a locale. This is to make it more obvious that everything in that locale should be in English - you could also name it default_locale, you just have to make sure to edit it in the Localization object.
  • We pass ctx to the localization function, which will automatically try getting ctx.guild.preferred_locale. This works with Interaction (by using Interaction.guild.preferred_locale internally), Guild (by using guild.preferred_locale internally), or just passing the Locale itself, for example, discord.Locale.american_english (which will convert to the en-US locale).

Working with plurals

from discord.ext.localization import Localization
import locale

_ = Localization("main_plurals.json") # this will look for main_plurals.json as the language JSON file
apples = int(input("How many apples do you have? >>> "))
language = locale.getlocale()[0][:2] # this will return the default OS language, such as en, hu, de, fr, es etc.

print(_.one("apples", apples, language, apples=apples))
{
    "en": {
        "apples": ["You only have one apple! What a loser...", "You have {apples} apples!"]
    },
    "hu": {
        "apples": ["{apples} almád van!"]
    }
}

This example does a great job at presenting how the .one() function works. Here's the explanation for the code:

The code will look for the default OS language, and will tell you how many apples you have with plurals in mind. I always get angry when I see "you have 1 apples"-ish text on a website, and it's an easy fix, but for some reason, many developers don't pay attention to it.

Hungarian doesn't make a difference between plurals, so the list for "hu"/"apples" only has 1 item. This won't be a problem though - the library isn't hardcoded to only look for the first and second elements in the returned list, it will look for the first and the last. Which means that if a list only has 1 item, it will only return that item.

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

discord_localization-1.0.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

discord_localization-1.0.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file discord_localization-1.0.0.tar.gz.

File metadata

  • Download URL: discord_localization-1.0.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for discord_localization-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4b06667f5109b87a049f2d7d8a50ddcf7fe5b57ebe632091df0ea9b13ac5a880
MD5 6474c9ac8f2637b4b80ee828e3a5b7f0
BLAKE2b-256 23740d7478cfa29a89aa5b4d22817ae75e5c02920273d712e1596b0b9d2d5bbf

See more details on using hashes here.

Provenance

File details

Details for the file discord_localization-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for discord_localization-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67517f8dbed46d91cc5d9de79ab602ae492c76d0239a8e34ac1f2c4d568c700d
MD5 bf9cdcfb047b742f1a5f96032f1818a3
BLAKE2b-256 e8be05025118f288197216a34c7f93a9606f87d4121223ce67ffc82b3beaf63f

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page