PhpBotFramework
2.0.2
A framework for Telegram Bots' APIs.
|
Methods to create a localized bot. More...
Functions | |
bool | PhpBotFramework\Localization\File::loadSingleLanguage (string $lang='en', string $dir='./localization') |
bool | PhpBotFramework\Localization\File::loadAllLanguages (string $dir='./localization') |
Load all localization files (like JSON) from a folder and set them in $local variable. More... | |
bool | PhpBotFramework\Localization\File::loadCurrentLocalization (string $dir='') |
PhpBotFramework\Localization\File::setLocalizationDir (string $dir) | |
Change source directory for localization files. More... | |
PhpBotFramework\Localization\Localization::__construct (BasicBot &$bot) | |
string | PhpBotFramework\Localization\Localization::getLanguageDatabase () |
Get current user's language from the database, and set it in $language. More... | |
bool | PhpBotFramework\Localization\Localization::setLanguageDatabase (string $language) |
Set the current user language in database and internally. More... | |
string | PhpBotFramework\Localization\Localization::getLanguageRedis (int $expiring_time=86400) |
Get current user language from Redis (as a cache) and set it in language. More... | |
bool | PhpBotFramework\Localization\Localization::setLanguageRedis (string $language, int $expiring_time=86400) |
Set the current user language in both Redis, database and internally. More... | |
string | PhpBotFramework\Localization\LocalizedString::getStr ($index) |
Get a localized string giving an index. More... | |
Variables | |
PhpBotFramework\Localization\File::$local | |
PhpBotFramework\Localization\File::$localization_dir = './localization' | |
Source for localization files. | |
PhpBotFramework\Localization\Localization::$bot | |
Reference to the bot. | |
PhpBotFramework\Localization\Localization::$user_table = 'User' | |
Table containing bot users data into database. | |
PhpBotFramework\Localization\Localization::$id_column = 'chat_id' | |
Name of the column that represents the user ID into database. | |
PhpBotFramework\Localization\Localization::$language | |
Current user/group language. | |
PhpBotFramework\Localization\LocalizedString::$bot | |
PhpBotFramework\Localization\LocalizedString::$language | |
PhpBotFramework\Localization\LocalizedString::$local | |
Methods to create a localized bot.
Create a localized bot.
Localization files must have this syntax: file ./localization/en.json
:
{"Hello_Msg": "Hello"}
File ./localization/it.json
:
{"Hello_Msg": "Ciao"}
Usage in processMessage()
:
$sendMessage($this->local[$this->language]["Hello_Msg"]);
Using both a sql database and a redis database you can develop a localized bot with just a small impact on the performance. By default the sql database will store the language permanently in a table which name is defined in $user_table. The redis database will cache the language for the chat_id for a variable time. You can use only the sql database if a redis database is not accessible. These methods will treat groups as User so they will be stored in the table as normal user does.
string PhpBotFramework\Localization\Localization::getLanguageDatabase | ( | ) |
Get current user's language from the database, and set it in $language.
string PhpBotFramework\Localization\Localization::getLanguageRedis | ( | int | $expiring_time = 86400 | ) |
Get current user language from Redis (as a cache) and set it in language.
Using Redis as cache, check for the language. On failure, get the language from the database and store it (with default expiring time of one day) in Redis.
It also change $language parameter of the bot to the language returned.
int | $expiring_time | Optional. Set the expiring time for the language on redis each time it is took from the sql database. |
string PhpBotFramework\Localization\LocalizedString::getStr | ( | $index | ) |
Get a localized string giving an index.
Using LocalizedString::language this method get the string of the index given localized string in language of the current user/group. This method will load the language first, using PhpBotFramework::getLanguage(), if the language has not been set. If the localization file for the user/group language has not been load yet, it will load it (load only the single localization file if the bot is using webhook, load all otherwise).
string | $index | Index of the localized string to get. |
bool PhpBotFramework\Localization\File::loadAllLanguages | ( | string | $dir = './localization' | ) |
Load all localization files (like JSON) from a folder and set them in $local
variable.
Save all localization files, using JSON format, from a directory and put the contents in $local
variable.
Each file will be saved into $local
with the first two letters of the filename as the index.
string | $dir | Source directory for localization files. |
bool PhpBotFramework\Localization\Localization::setLanguageDatabase | ( | string | $language | ) |
Set the current user language in database and internally.
Save it into database first.
string | $language | The language to set. |
bool PhpBotFramework\Localization\Localization::setLanguageRedis | ( | string | $language, |
int | $expiring_time = 86400 |
||
) |
Set the current user language in both Redis, database and internally.
Save it into database first, then create the expiring key on Redis.
string | $language | The language to set. |
int | $expiring_time | Optional. Time for the language key in redis to expire. |
PhpBotFramework\Localization\File::setLocalizationDir | ( | string | $dir | ) |
Change source directory for localization files.
string | $dir | Source directory. |