PhpBotFramework  2.0.2
A framework for Telegram Bots' APIs.
Functions | Variables
Localization

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
 

Detailed Description

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.

Function Documentation

◆ getLanguageDatabase()

string PhpBotFramework\Localization\Localization::getLanguageDatabase ( )

Get current user's language from the database, and set it in $language.

Returns
string Language set for the current user, throw error if there is language is not set for the user.

◆ getLanguageRedis()

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.

Parameters
int$expiring_timeOptional. Set the expiring time for the language on redis each time it is took from the sql database.
Returns
string Language for the current user, $default_language on errors.

◆ getStr()

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

Parameters
string$indexIndex of the localized string to get.
Returns
string Localized string in the current user/group language.

◆ loadAllLanguages()

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.

Parameters
string$dirSource directory for localization files.
Returns
bool True if the directory could be opened without errors.

◆ setLanguageDatabase()

bool PhpBotFramework\Localization\Localization::setLanguageDatabase ( string  $language)

Set the current user language in database and internally.

Save it into database first.

Parameters
string$languageThe language to set.
Returns
bool On sucess, return true, throws exception otherwise.

◆ setLanguageRedis()

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.

Parameters
string$languageThe language to set.
int$expiring_timeOptional. Time for the language key in redis to expire.
Returns
bool On sucess, return true, throws exception otherwise.

◆ setLocalizationDir()

PhpBotFramework\Localization\File::setLocalizationDir ( string  $dir)

Change source directory for localization files.

Parameters
string$dirSource directory.