1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PhpBotFramework. |
5
|
|
|
* |
6
|
|
|
* PhpBotFramework is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation, version 3. |
9
|
|
|
* |
10
|
|
|
* PhpBotFramework is distributed in the hope that it will be useful, but |
11
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13
|
|
|
* Lesser General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace PhpBotFramework\Localization; |
20
|
|
|
|
21
|
|
|
use PhpBotFramework\Exceptions\BotException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* \class File Localization Files handler |
25
|
|
|
* \brief Handle localizations files, load them from disk. |
26
|
|
|
*/ |
27
|
|
|
trait File |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* \addtogroup Localization Localization |
31
|
|
|
* \brief Methods to create a localized bot. |
32
|
|
|
* \details Localization files must have this syntax: |
33
|
|
|
* file <code>./localization/en.json</code>: |
34
|
|
|
* |
35
|
|
|
* {"Hello_Msg": "Hello"} |
36
|
|
|
* |
37
|
|
|
* File <code>./localization/it.json</code>: |
38
|
|
|
* |
39
|
|
|
* {"Hello_Msg": "Ciao"} |
40
|
|
|
* |
41
|
|
|
* Usage in <code>processMessage()</code>: |
42
|
|
|
* |
43
|
|
|
* $sendMessage($this->local[$this->language]["Hello_Msg"]); |
44
|
|
|
* |
45
|
|
|
* @{ |
46
|
|
|
*/ |
47
|
|
|
|
48
|
|
|
/** @internal |
49
|
|
|
* \brief Store the localized strings. */ |
50
|
|
|
protected $local; |
51
|
|
|
|
52
|
|
|
/** \brief Source for localization files. */ |
53
|
|
|
protected $localization_dir = './localization'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @internal |
57
|
|
|
* \brief Load a localization file into the localized strings array. |
58
|
|
|
* @param string $lang Language to load. |
59
|
|
|
* @param string $dir Directory in which there are the JSON files. |
60
|
|
|
* @return bool True if loaded. |
61
|
|
|
*/ |
62
|
|
|
protected function loadSingleLanguage(string $lang = 'en', string $dir = './localization') : bool |
63
|
|
|
{ |
64
|
|
|
// Name of the file |
65
|
|
|
$filename = "$dir/$lang"; |
66
|
|
|
|
67
|
|
|
// If this language isn't already set |
68
|
|
|
if (!isset($this->local[$lang])) { |
69
|
|
|
// and the file exists |
70
|
|
|
if (file_exists($filename)) { |
71
|
|
|
// Load localization in memory |
72
|
|
|
$this->local[$lang] = json_decode(file_get_contents($filename), true); |
73
|
|
|
|
74
|
|
|
// We loaded it |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
// The file doens't exists |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// The language is already set |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* \brief Load all localization files (like JSON) from a folder and set them in <code>$local</code> variable. |
87
|
|
|
* \details Save all localization files, using JSON format, from a directory and put |
88
|
|
|
* the contents in <code>$local</code> variable. |
89
|
|
|
* |
90
|
|
|
* Each file will be saved into <code>$local</code> with the first two letters of the filename as the index. |
91
|
|
|
* @param string $dir Source directory for localization files. |
92
|
|
|
* @return bool True if the directory could be opened without errors. |
93
|
|
|
*/ |
94
|
|
|
public function loadAllLanguages(string $dir = './localization') : bool |
95
|
|
|
{ |
96
|
|
|
if ($handle = opendir($dir)) { |
97
|
|
|
// Iterate over all files |
98
|
|
|
while (false !== ($file = readdir($handle))) { |
99
|
|
|
// If the file is a JSON data file |
100
|
|
|
if (strlen($file) > 6 && substr($file, -5) === '.json') { |
101
|
|
|
try { |
102
|
|
|
// Add the contents of the file to the $local variable, after converting it to a PHP object. |
103
|
|
|
// The contents will be added with the 2 letter of the file as the index |
104
|
|
|
$this->local[substr($file, 0, 2)] = json_decode(file_get_contents("$dir/$file"), true); |
105
|
|
|
} catch (BotException $e) { |
106
|
|
|
echo $e->getMessage(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @internal |
117
|
|
|
* \brief Load localization for current language and mode. |
118
|
|
|
* \details This method will load only the current user/group localization file if the bot is using webhook, all files otherwise. |
119
|
|
|
* @param string $dir Directory where the localization file is stored. If no directory is given (by default) it will load it from $localization_dir (which is ./localization if it is not set). |
120
|
|
|
* @return bool True if the localization has been loaded or it is already loaded. |
121
|
|
|
*/ |
122
|
|
|
public function loadCurrentLocalization(string $dir = '') : bool |
123
|
|
|
{ |
124
|
|
|
// If no dir has been given |
125
|
|
|
if (!isset($dir) || $dir === '') { |
126
|
|
|
// The dir is the default one |
127
|
|
|
$dir = $this->localization_dir; |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// If the language of the user is already set in the array containing localizated strings |
131
|
|
|
if (!isset($this->local[$this->bot->language])) { |
132
|
|
|
// Is the bot using webhook? |
133
|
|
|
if (isset($this->bot->_is_webhook)) { |
|
|
|
|
134
|
|
|
return $this->loadSingleLanguage($this->localization_dir); |
135
|
|
|
} else { |
136
|
|
|
return $this->loadAllLanguages($this->localization_dir); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* \brief Change source directory for localization files. |
145
|
|
|
* @param string $dir Source directory. |
146
|
|
|
*/ |
147
|
|
|
public function setLocalizationDir(string $dir) |
148
|
|
|
{ |
149
|
|
|
$this->localization_dir = $dir; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** @} */ |
153
|
|
|
} |
154
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.