cb-i18n
Advanced internationalisation (i18n) support for Discord bots
This module works with any well-known Discord API library for Python, including:
- discord.py (and any derived libraries such as disnake, nextcord and pycord)
- hikari.py
Installation
Install with pip:
# Linux / MacOS
python3 -m pip install cb-i18n
# Windows
py -3 -m pip install cb-i18n
Install from source:
git clone https://github.com/LTT/cb-i18n
cd cb-i18n
python3 setup.py install
# Windows
git clone https://github.com/LTT/cb-i18n
cd cb-i18n
py -3 setup.py install
Example usage of cb-i18n:
In fact, this library can work with just any application written in python. The first arg in Translator.translate can be anything, you can pass integer, string, custom object or even module, all will work as expected by you. Internally, lib does nothing with that "context", it just passing it to your locale_getter and all.
Directory structure:
src/
languages/
en-US.json
ru-RU.json
main.py
src/languages/en-US.json:
{
"Hello {}": "Hello {}", // Positional placeholders
"You're a Genius!": "You're a Genius!", // No placeholders
"Locale is now {locale}": "Locale is now {locale}" // Keyword-only placeholders
}
src/languages/ru-RU.json:
{
"Hello {}": "Привет {}",
"You're a Genius!": "Да ты гений!",
"Locale is now {locale}": "Язык теперь {locale}"
}
src/main.py:
import sqlite3
import disnake
from disnake.ext import commands
import cb_i18n as i18n
bot = commands.Bot("!", intents=disnake.Intents.all())
connection = sqlite3.connect('mydatabase.db')
cursor = connection.cursor()
translator = None
_ = None
def get_locale(ctx: commands.Context):
return cursor.execute("SELECT locale FROM guilds WHERE id = ?;", ctx.guild.id).fetchone()[0]
@bot.event
async def on_ready():
print("Bot loading")
cursor.execute("CREATE TABLE IF NOT EXISTS guilds (id bigint, locale text);")
connection.commit()
print(" Loading guilds table...")
for guild in bot.guilds:
if not cursor.execute("SELECT * FROM guilds WHERE id = ?", guild.id).fetchone():
print(" Guild {}: not exist".format(guild.id))
cursor.execute("INSERT INTO guilds VALUES (?, ?);", guild.id, "en-US")
connection.commit()
else:
print(" Guild {}: exists".format(guild.id))
print(" Loaded guilds table.")
print(" Loading i18n...")
global translator, _
translator = i18n.make_translator()
translator.set_locale_dir("./languages/")
translator.set_locale_getter(get_locale)
translator.load_translations()
_ = translator.translate
print(" Loaded i18n.")
print("Bot ready!\nAccount: {} (ID: {})\nGuild count: {}\nUser count: {}".format(str(bot.user), bot.user.id, len(bot.guilds), len(bot.users)))
@bot.command(description="Set locale for a guild", aliases=["setlocale", "set-locale"])
async def set_locale(ctx: commands.Context, locale: str):
cursor.execute("UPDATE guilds SET locale = ? WHERE id = ?;", locale, ctx.guild.id)
connection.commit()
return await ctx.send(_(ctx, "Locale is now {locale}").format(locale=locale))
@bot.command(description="Say hi!")
async def hi(ctx: commands.Context, name: str):
return await ctx.send(_(ctx, "Hello {}").format(name))
@bot.command(description="You're Genius, right?")
async def genius(ctx: commands.Context):
return await ctx.send(_(ctx, "You're a Genius!"))
try:
bot.run(os.environ.get("BOT_TOKEN"))
except Exception as exc:
print(exc.__class__.__name__ + ": " + exc.__str__() + "".join(tb.format_exception(exc)))
finally:
cursor.close()
connection.close()
Metadata
Release files for cb-i18n 1.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cb_i18n-1.0.4.tar.gz | 5.2 kB | Without provenance | Details for cb_i18n-1.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cb_i18n-1.0.4-py3-none-any.whl | Python 3 | none | any | Details for cb_i18n-1.0.4-py3-none-any.whl |
Total release size: 10.7 kB
Release files / cb_i18n-1.0.4.tar.gz
| Download URL | cb_i18n-1.0.4.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bce4dc5fb11221b4b61273a460d83d5357339894e041f7fe441568f89555d70d
|
|
BLAKE2b-256 checksum How to use checksums |
e03ac8cccc91c58526e4ba4fb6099ae3b2d143e72b5ab02974596ddda8fcc283
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.10.2
|
Release files / cb_i18n-1.0.4-py3-none-any.whl
| Download URL | cb_i18n-1.0.4-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6a87211c2a8c51b0cd7ec7ca0d80cffeb3af8383f3c6f5d87cdc4dbc8807a757
|
|
BLAKE2b-256 checksum How to use checksums |
6bbb0c3f099532a601be6c280512cb24c5ca9462e0762fec33af18f006fcf531
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.10.2
|
Security for cb-i18n 1.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.