Lang File
Introduction
Language files are a crucial part of developing a multilingual bot using SmartBot
. They allow you to separate the textual content from the logic of your bot, making it easier to manage and update translations.
This guide will walk you through the process of creating and setting up a language file for SmartBot
.
Structure
A language file in SmartBot
is a JSON object that contains keys and values. The keys represent command names, message types, or other identifiers, while the values hold the actual text or translations.
Basic Structure
Rules for Writing Language Files
Do Not Translate Keys: Never translate the keys in the language file; only translate the values.
Preserve Masks in Curly Braces: Any text within
{}
should not be translated. These are placeholders for dynamic content.Avoid Changing the Structure: Maintain the JSON structure as defined in your bot's code.
Adding Commands and Responses
Each command your bot can execute should have a corresponding entry in the language file. For example:
Command Structure in Language Files
In SmartBot
, the commands section of the language file plays a crucial role. Each command the bot can execute should have a corresponding entry in this section. The structure allows for dynamic content insertion and supports various functionalities like text responses, keyboard layouts, and media handling.
Basic Template for Commands
Each command entry can include the following properties:
Key Properties Explained
text: The bot's response text. You can embed variables like
{username}
that will be dynamically replaced.parse_mode: Defines how the message text should be parsed and formatted. Defaults to "Markdown".
chat_id: Specifies where the message should be sent. It can be a user ID, a group ID, or a channel username. By default - it is current chat.
alias and aliases: Shortcuts or alternative names for commands. Useful for multi-language support or creating intuitive command names. Examples: "Back, Cancel"
keyboard and inline_buttons: Define custom keyboards or inline button layouts for interactive responses.
alert and alert_top: Special properties for displaying alerts when inline buttons are pressed.
photo: Directs the bot to send an image. The URL should point to the image file.
edit: A boolean flag indicating whether the bot should edit its previous message instead of sending a new one.
message_id: message id for editing. Please note: SmartBot passed current message id on inline button pressing automatically
Example: Simple Test Command
Here's a basic example of a test command setup in the language file:
Using Types and References
You can also use predefined types or templates defined in the types
section of your language file, as shown in the example above with #/langVer
.
then you can use types in commands:
and as keyboard:
Setting Up the Language File in SmartBot
To support multiple languages, create separate language files for each language, and then use SmartBot
's setupLng
method to load the appropriate file based on user preferences.
Once your language file is ready, you can set it up in SmartBot
like this - command name "lng-en":
another command - 'lng-fr':
Setup command - /setup:
Then run /setup command in the bot.
if you change Lang file you need to rerun /setup
again
Last updated