1 | <?php |
||
23 | trait File |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * \addtogroup Localization Localization |
||
28 | * \brief Methods to create a localized bot. |
||
29 | * \details Localization files must have this syntax: |
||
30 | * file <code>./localization/en.json</code>: |
||
31 | * |
||
32 | * {"Hello_Msg": "Hello"} |
||
33 | * |
||
34 | * File <code>./localization/it.json</code>: |
||
35 | * |
||
36 | * {"Hello_Msg": "Ciao"} |
||
37 | * |
||
38 | * Usage in <code>processMessage()</code>: |
||
39 | * |
||
40 | * $sendMessage($this->local[$this->language]["Hello_Msg"]); |
||
41 | * |
||
42 | * @{ |
||
43 | */ |
||
44 | |||
45 | /** \brief (<i>Internal</i>)Store the localizated strings. */ |
||
46 | protected $local; |
||
47 | |||
48 | /** \brief Directory where there are the localization files. */ |
||
49 | protected $localization_dir = './localization'; |
||
50 | |||
51 | /** |
||
52 | * \brief (<i>Internal</i>)Load a localization file into the localized strings array. |
||
53 | * @param string $lang Language to load. |
||
54 | * @param string $dir Directory in which there are the JSON files. |
||
55 | * @return bool True if loaded. |
||
56 | */ |
||
57 | protected function loadSingleLanguage(string $lang = 'en', string $dir = './localization') : bool |
||
79 | |||
80 | /** |
||
81 | * \brief Load all localization files (JSON-serialized) from a folder and set them in $local variable. |
||
82 | * \details Save all localization files, saved as json format, from a directory and put the contents in $local variable. |
||
83 | * Each file will be saved into $local with the first two letters of the filename as the index. |
||
84 | * @param string $dir Directory where the localization files are saved. |
||
85 | * @return bool True if the directory could be opened without errors. |
||
86 | */ |
||
87 | public function loadAllLanguages(string $dir = './localization') : bool |
||
88 | { |
||
89 | // Open directory |
||
90 | if ($handle = opendir($dir)) { |
||
91 | // Iterate over all files |
||
92 | while (false !== ($file = readdir($handle))) { |
||
93 | // If the file is a JSON data file |
||
94 | if (strlen($file) > 6 && substr($file, -5) === '.json') { |
||
95 | try { |
||
96 | // Add the contents of the file to the $local variable, after deserializng it from JSON format |
||
97 | // The contents will be added with the 2 letter of the file as the index |
||
98 | $this->local[substr($file, 0, 2)] = json_decode(file_get_contents("$dir/$file"), true); |
||
99 | } catch (BotException $e) { |
||
100 | echo $e->getMessage(); |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | return true; |
||
105 | } |
||
106 | return false; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * \brief Load localization for current language and mode. |
||
111 | * \details This method will load only the current user/group localization file if the bot is using webhook, all files otherwise. |
||
112 | * @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). |
||
113 | * @return bool True if the localization has been loaded or it is already loaded. |
||
114 | */ |
||
115 | public function loadCurrentLocalization(string $dir = '') : bool |
||
135 | |||
136 | /** |
||
137 | * \brief Set location of localization files. |
||
138 | * @param string $dir Directory where the files are stored. |
||
139 | */ |
||
140 | public function setLocalizationDir(string $dir) |
||
144 | |||
145 | /** @} */ |
||
146 | } |
||
147 |
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.