Skip to main content

Localization for the discord.py fork, pycord

Project description

Pycord18n

Ported by YoungTrep for the new Discord.py fork - Pycord

This is a open sourced version of the internal internationalization engine used for Kolumbao.

Installation

To install the Pycord18n, you can just run the following command:

# Windows
py -m pip install pycord18n

# Linux/MacOS
python3 -m pip install pycord18n

You can now use the library!

Usage

Setting up languages

A language can be initialized like this:

french = Language("French", "fr", {
    "hello": "Bonjour",
    "goodbye": "Au revoir",
    "francais": "Français"
})

But you may want to store languages seperately and create them as follows:

import json
french = Language("French", "fr", json.load(open("fr.json")))

Base I18n class

When setting up the i18n class, we need to setup our languages and declare a fallback language:

i18n = I18n([
    Language("English", "en", {
        "hello": "Hello",
        "goodbye": "Goodbye",
        "english": "English"
    }),
    Language("French", "fr", {
        "hello": "Bonjour",
        "goodbye": "Au revoir",
        "francais": "Français"
    }),
], fallback="en")

i18n will now fallback to english if it can't find a translation for other languages.

>>> i18n.get_text("hello", "en")
'Hello'
>>> i18n.get_text("hello", "fr")
'Bonjour'
>>> # "english" is not a listed translation in the French locale, so we revert to english
>>> i18n.get_text("english", "fr")
'English'
>>> # However we can make it not fallback, but this will throw an error if the translation isn't found
>>> i18n.get_text("english", "fr", should_fallback=False) 
Traceback (most recent call last):
  ...      
py18n.i18n.InvalidTranslationKeyError: 'Translation foo not found for en!'

Discord

For Pycord, we can use the extension py18n.extension.I18nExtension. Setup your bot as you would usually, and then run i18n.init_bot as follows.

from discord.ext import commands
from py18n.extension import I18nExtension

# Make our bot
bot = commands.Bot("prefix")

# Setup similarly to the usual class
i18n = I18nExtension([
    Language("English", "en", {
        "hello": "Hello",
        "goodbye": "Goodbye",
        "english": "English"
    }),
    Language("French", "fr", {
        "hello": "Bonjour",
        "goodbye": "Au revoir",
        "francais": "Français"
    }),
], fallback="en")

# Setup the bot by giving it a function to get the user's locale.
# This could potentially refer to a database or other file.
# Anything you want!
# Otherwise, it will always be the fallback locale.
def get_locale(ctx: commands.Context):
    preferences = {
       301736945610915852: "en"
    }
    return preferences[ctx.author.id]

# Set it up!
i18n.init_bot(bot, get_locale)

@bot.command(pass_context=True)
async def hello(ctx):
    await ctx.send(i18n.contextual_get_text("hello"))

This is all good, but because of our line i18n.init_bot(bot, get_locale), we can shorten things.

This function adds a pre-invoke hook that sets the language based on the result of get_locale. The contextually_get_text function is also exposed as py18n.extension._, and it is a classmethod.

We can change it by adding the following import and change our function:

from py18n.extension import I18nExtension, _

# ...

@bot.command(pass_context=True)
async def hello(ctx):
    await ctx.send(_("hello"))

There, much tidier!

  • The _ function considers the current context and uses the correct locale by default.
  • When initializing any I18nExtension, as we did earlier, it becomes the default i18n instance. The default instance is used by _ and contextually_get_text.

Issues

If you encounter any problems, check out current issues or make a new issue.

Notes

  • Feel free to contribute! This is released under the GLP-3 license. (If you suggest another license, make an issue suggesting).

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

pycord18n-1.0.3.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

pycord18n-1.0.3-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file pycord18n-1.0.3.tar.gz.

File metadata

  • Download URL: pycord18n-1.0.3.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for pycord18n-1.0.3.tar.gz
Algorithm Hash digest
SHA256 41f9aae38b4a39b7e43ae289883570c275ffbfd884217580d237772b39d8eeae
MD5 736df874e3a5c63079547294fd761153
BLAKE2b-256 7c3bef57832f6657ed949ded1669ae8d8f2d389e0dc9823ca4695f96f20c5b67

See more details on using hashes here.

File details

Details for the file pycord18n-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: pycord18n-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 22.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for pycord18n-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2e180b158933c8cd7fb6fffe58c4f4ec86ded36246cfd753cd287377719a7871
MD5 b23e4639569eff084fbc2ee8a91822eb
BLAKE2b-256 0db05fa9dbafb793a6097428e6dc90a3246b54185a3abd22815de23560cdf570

See more details on using hashes here.

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