Internationalization Framework

Basic Usage

from redbot.core import commands
from redbot.core.i18n import Translator, cog_i18n

_ = Translator("ExampleCog", __file__)

@cog_i18n(_)
class ExampleCog:
    """description"""

    @commands.command()
    async def mycom(self, ctx):
        """command description"""
        await ctx.send(_("This is a test command"))

Tutorial

After making your cog, generate a messages.pot file

We recommend using redgettext - a modified version of pygettext for Red. You can install redgettext by running pip install redgettext in a command prompt.

To generate the messages.pot file, you will now need to run python -m redgettext -c [path_to_cog] This file will contain all strings to be translated, including docstrings. (For advanced usage check python -m redgettext -h)

You can now use a tool like poedit to translate the strings in your messages.pot file.

API Reference

class redbot.core.i18n.Translator(name, file_location)[source]

Bases: Callable[[str], str]

Function to get translated strings at runtime.

__call__(untranslated)[source]

Translate the given string.

This will look for the string in the translator’s .pot file, with respect to the current locale.

load_translations()[source]

Loads the current translations.

redbot.core.i18n.cog_i18n(translator)[source]

Get a class decorator to link the translator to this cog.

redbot.core.i18n.get_babel_locale(locale=None)[source]

Function to convert a locale to a babel.core.Locale.

Parameters:

locale (Optional[str]) – The locale to convert, if not specified it defaults to the bot’s locale.

Returns:

The babel locale object.

Return type:

babel.core.Locale

redbot.core.i18n.get_babel_regional_format(regional_format=None)[source]

Function to convert a regional format to a babel.core.Locale.

If regional_format parameter is passed, this behaves the same as get_babel_locale.

Parameters:

regional_format (Optional[str]) – The regional format to convert, if not specified it defaults to the bot’s regional format.

Returns:

The babel locale object.

Return type:

babel.core.Locale

redbot.core.i18n.get_locale()[source]

Get locale in a current context.

Returns:

Current locale’s language code with country code included, e.g. “en-US”.

Return type:

str

await redbot.core.i18n.get_locale_from_guild(bot, guild)[source]

Get locale set for the given guild.

Parameters:
  • bot (Red) – The bot’s instance.

  • guild (Optional[discord.Guild]) – The guild contextual locale is set for. Use None if the context doesn’t involve guild.

Returns:

Guild locale’s language code with country code included, e.g. “en-US”.

Return type:

str

redbot.core.i18n.get_regional_format()[source]

Get regional format in a current context.

Returns:

Current regional format’s language code with country code included, e.g. “en-US”.

Return type:

str

await redbot.core.i18n.get_regional_format_from_guild(bot, guild)[source]

Get regional format for the given guild.

Parameters:
  • bot (Red) – The bot’s instance.

  • guild (Optional[discord.Guild]) – The guild contextual locale is set for. Use None if the context doesn’t involve guild.

Returns:

Guild regional format’s language code with country code included, e.g. “en-US”.

Return type:

str

await redbot.core.i18n.set_contextual_locales_from_guild(bot, guild)[source]

Set contextual locales (locale and regional format) for given guild context.

Parameters:
  • bot (Red) – The bot’s instance.

  • guild (Optional[discord.Guild]) – The guild contextual locale is set for. Use None if the context doesn’t involve guild.