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

The process of generating this will depend on the operating system you are using

In a command prompt in your cog’s package (where yourcog.py is), create a directory called “locales”. Then do one of the following:

Windows: python <your python install path>\Tools\i18n\pygettext.py -D -n -p locales

Mac: ?

Linux: pygettext3 -D -n -p locales

This will generate a messages.pot file with strings to be translated, including docstrings.

API Reference

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

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

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

Bases: collections.abc.Callable, typing.Generic

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.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