Skip to content

Localizing commands

Mmesek edited this page Jun 5, 2022 · 2 revisions

Generating translation files from command metadata

Simply run MFramework with -update-translation flag, it will generate translation files with command name, description, arguments and choices for each found locale and then exit

If you don't have any locale yet, create empty directory named en-US in locale folder to get started

Example: python3 -m MFramework bot -update-translation

Translation from scratch

  1. Go to locale
  2. Copy en and paste
  3. Rename to another locale's language code, preferable according to this table: https://discord.com/developers/docs/reference#locales
  4. Dive into each file you find there and rewrite values (right hand side to :) into your language.

Updating existing translations

  1. Run MFramework with -update-translation flag: python3 -m MFramework bot -update-translation
  2. Check diff of changes
  3. Edit changes

Additional guidelines for translating:

  • Keys has to stay as-is
  • Internalization works based on python-18n module:
    • Placeholders are marked with %{} like: %{variable} and their content shouldn't be translated
    • If a string can be pluralized, like one, few or many, value should be a mapping containing keys for zero, one, fewand many and respective values for singular/plural forms:
      "key":{
          "zero": "Nothing",
          "one": "One key",
          "few": "Only %{count} keys",
          "many": "${count} keys!" 
      }

Localization's Usage

  • If you want to use your translated string in code, you can do so in command's function by using ctx.t("key", count=5, another_placeholder="your_name"). Note however, this method is automatically choosing Context's language as well as setting up a namespace of your localization (therefore, if your key is nested in locale according to function's path like bot.commands.stuff.command_name.key you only need to supply "key" as a localization key).
  • If there is no Context available, you can use translate function from utils.localizations. This method doesn't know the namespace or language therefore full key has to be supplied.