Localization

Localized strings

As Anjani supports multi-language, localized string placed on anjani/language/ with a .yml file.

Use Localized Strings

~Plugin.get_text():

Parse the string with the user language setting. Return English string in case other language doesn't have the string requested.

Bound method can be use with ~Context.get_text() whose chat_id already set.

Parameters

  • chat_id (int) - Id of the sender if in PM's or chat_id to fetch the user language setting.
  • name (str) - String name(key) to parse.
  • *args (any, Optional) - One or more values that should be formatted and inserted in the string. The value should be in order based on the language string placeholder.
  • noformat (bool, Optional) - If exist and True, the text returned will not be formatted (if you don't want to insert any value to the placeholders). Default to False.
  • **kwargs (any, Optional) - One or more keyword values that should be formatted and inserted in the string. Based on the keyword placeholder on the language strings.

Example

Bellow is the string on the en.yaml file.

test-string: Good {} my name is {my_name}.

To parse the string, simply call the text method via Plugin instance ~Plugin.get_text().

from anjani import command, plugin


class ExampleText(plugin.Plugin):
    name = "Example Text"

    async def cmd_test(self, ctx: command.Context) -> str:
        return await self.get_text(
            ctx.chat.id, "test-string", "morning", my_name="Anjani"
        )
        # The Message text will be "Good morning my name is Anjani."

Or using via Context instance ~Context.get_text().

from anjani import command, plugin


class ExampleText(plugin.Plugin):
    name = "Example Text"

    async def cmd_test(self, ctx: command.Context) -> str:
        return await ctx.get_text(
            "test-string", "morning", my_name="Anjani"
        )
        # The Message text will be "Good morning my name is Anjani."