Skip to content

Commit 69f32c6

Browse files
committed
Added Translator class
1 parent 438899c commit 69f32c6

7 files changed

Lines changed: 140 additions & 22 deletions

File tree

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
"autoload": {
2424
"psr-4": {
2525
"TelegramSDK\\BotAPI\\": "src/"
26-
}
26+
},
27+
"files": [
28+
"src/Utils/functions.php"
29+
]
2730
},
2831
"autoload-dev": {
2932
"psr-4": {

docs/00-introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use TelegramSDK\BotAPI\Telegram\Bot;
1818
$bot = new Bot("YOUR_BOT_TOKEN"); // Your bot token
1919

2020
$bot->sendMessage([ // Send a message
21-
"chat_id" => 123 // Your chat id
21+
"chat_id" => 123, // Your chat id
2222
"text" => "A message"
2323
]);
2424
```

docs/02-translating.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Translating
2+
This library provides a class to translate texts from a given object.
3+
4+
## How to use
5+
Assuming you have a file called `locales/en.json`:
6+
```json
7+
{
8+
"start":{
9+
"text": "<b>Hi {{MENTION}}!</b>",
10+
"keyboard": {
11+
"inline_keyboard": [
12+
[{
13+
"text": "Awesome Library",
14+
"url": "https://github.com/TelegramSDK/BotAPI"
15+
}]
16+
]
17+
}
18+
}
19+
}
20+
```
21+
22+
23+
```php
24+
<?php
25+
use TelegramSDK\BotAPI\Utils\Translator;
26+
27+
$user = $updates->message->from;
28+
29+
$user_language = $user->language_code ?? "en";
30+
31+
if(file_exists("locales/$user_language.json")){
32+
$file_texts = json_decode(file_get_contents("locales/$user_language.json"));
33+
} else{
34+
$file_texts = json_decode(file_get_contents("locales/en.json"));
35+
}
36+
37+
$translator = new Translator($file_texts, [
38+
"{{MENTION}}" => "<a href='tg://user?id=$user->id'>$user->first_name</a>"
39+
]);
40+
41+
$bot->sendMessage([
42+
"chat_id" => $user->id,
43+
"text" => $translator->start->text,
44+
"reply_markup" => json_encode($translator->texts->start->keyboard), // Calling $translator->texts doesn't translate the string and doesn't return a new instance of Translator
45+
"parse_mode" => "HTML"
46+
]);
47+
48+
```
49+

src/Telegram/Bot.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace TelegramSDK\BotAPI\Telegram;
1515

16-
use TelegramSDK\BotAPI\Utils;
1716
use TelegramSDK\BotAPI\Exceptions\TelegramException;
1817
use GuzzleHttp\Client as Guzzle;
1918
use GuzzleHttp\Exception\RequestException;
@@ -39,7 +38,7 @@ class Bot{
3938
*/
4039
public function __construct(string $token, int $updatesMethod = self::NO_UPDATES){
4140
$this->token = $token;
42-
$this->isProduction = Utils::isProduction();
41+
$this->isProduction = \TelegramSDK\BotAPI\Utils\isProduction();
4342
$this->updatesMethod = $updatesMethod;
4443

4544
if(!$this->isProduction){ // Assuming that you've tested the bot before pushing to production

src/Utils.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Utils/Translator.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Translator class.
4+
* This class provides a way to translate texts by replacing specific strings with their corresponding translations.
5+
*
6+
* @author Sebastiano Racca
7+
* @package TelegramSDK\BotAPI\Utils
8+
* @see docs/02-translating.md
9+
*/
10+
11+
declare(strict_types=1);
12+
namespace TelegramSDK\BotAPI\Utils;
13+
14+
15+
class Translator{
16+
public ?object $texts;
17+
public ?array $translations;
18+
19+
/**
20+
* Translator constructor.
21+
*
22+
* @param object $texts The object containing the original texts.
23+
* @param array|null $translations Associative array of the translations to be applied.
24+
*/
25+
public function __construct(object $texts, ?array $translations = null){
26+
$this->translations = $translations;
27+
$this->texts = $texts;
28+
}
29+
30+
/**
31+
* Translates the given string.
32+
*
33+
* @param string $text The text to be translated.
34+
* @return string The translated text.
35+
*/
36+
public function translate(string $text): string{
37+
return strtr($text, $this->translations);
38+
}
39+
40+
/**
41+
* Magic method to dynamically access properties.
42+
*
43+
* @param string $name The name of the property.
44+
* @return mixed The value of the property, translated if applicable.
45+
*/
46+
public function __get(string $name): mixed{
47+
if(!property_exists($this->texts, $name))
48+
return null;
49+
50+
$propertyValue = $this->texts->$name;
51+
52+
if(is_string($propertyValue))
53+
return $this->translate($propertyValue);
54+
55+
if(is_object($propertyValue) || is_array($propertyValue)){
56+
$translator = new Translator($this->texts, $this->translations);
57+
$translator->texts = $propertyValue;
58+
return $translator;
59+
}
60+
61+
return $propertyValue;
62+
}
63+
64+
}

src/Utils/functions.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* This file provides a set of util functions for the library.
4+
*
5+
* @author Sebastiano Racca
6+
* @package TelegramSDK\BotAPI\Utils
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace TelegramSDK\BotAPI\Utils;
12+
13+
14+
/**
15+
* Checks if the application is running in production mode.
16+
*
17+
* @return bool True if in production mode, false otherwise.
18+
*/
19+
function isProduction(): bool{
20+
return (defined('\PRODUCTION') && \PRODUCTION === true);
21+
}

0 commit comments

Comments
 (0)