-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trans.php
385 lines (357 loc) · 11 KB
/
trans.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/**
* PHP Server Monitor Language Editor
* Helps to maintain the translations files of PHP Server Monitor.
*
* This file is part of PHP Server Monitor Language Editor (PSMLE).
* PHP Server Monitor Language Editor is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* PHP Server Monitor Language Editor is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
*
* PHP versions 4, 5, 7 supported
*
* @category PHP
* @package Phpservermon-Language-Editor
* @author Tim Zandbergen <Tim@Xervion.nl>
* @copyright 2018 Tim Zandbergen
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version GIT: 0.8.1
* @link http://www.github.com/TimZ99/phpservermon-language-editor/
*
* @todo menu to select the translation file
* @todo Feature: export directly to Github
**/
//debug
ini_set('display_errors', 1);
error_reporting(E_USER_ERROR);
//error_reporting(E_ALL);
if (!is_readable('./config.php')) {
trigger_error("Config doesn't exist or is not readable.", E_USER_ERROR);
}
require './config.php';
/**
* Get the names of the files in the directory.
*
* @var array|boolean
*/
$files = scandir($path);
/**
* Disable input field if the input can not be saved.
*
* @var string
*/
$disable = '';
//check if path is directory
if ($files === false) {
trigger_error("Path $path is not a directory.", E_USER_ERROR);
}
//check if default lang exists
if (!in_array('en_US.lang.php', /** @scrutinizer ignore-type */ $files)) {
trigger_error("Default lang en_US not found.", E_USER_ERROR);
}
//check if translation file exists
if (!in_array($translationLang, /** @scrutinizer ignore-type */ $files)) {
trigger_error("$translationLang not found.", E_USER_ERROR);
}
//check if default lang file is readable
if (!is_readable($path . '/en_US.lang.php')) {
trigger_error("Default lang file not readable.", E_USER_ERROR);
}
//check if translation file is readable
if (!is_readable($path . "/" . $translationLang)) {
trigger_error("$translationLang not readable.", E_USER_ERROR);
}
//get content of default lang
require $path . '/en_US.lang.php';
/**
* Containing all default translations.
* Default: en_US.
*
* @var array
*/
$default = $sm_lang;
unset($sm_lang);
//get content of translated lang
require $path . "/" . $translationLang;
/**
* Containing all translations from translation file.
*
* @var array
*/
$translation = $sm_lang;
unset($sm_lang);
/**
* Making sure the value won't break the file.
*
* @param string $value Value to be processed.
*
* @return string
*/
function processValue($value)
{
$value = htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
$value = preg_replace("/((\\')|(\'))/", "\'", $value);
return $value;
}
/**
* Set style for input field:
* - if key doesn't exists in translation -> border red.
* - if translation and default are the same -> border orange.
* - else -> border default.
*
* @param string $key Array key.
* @param array $translation Array containing translation.
* @param string $value Default translation.
*
* @return string
*/
function setStyle($key, $translation, $value)
{
if (array_key_exists($key, $translation) && !empty($translation[$key])) {
if (processValue($translation[$key]) == $value) {
return 'border: 1px orange solid;';
}
return '';
}
return 'border: 1px red solid;';
}
/**
* Return translation value.
*
* @param string $key Array key.
* @param array $translation Array containing translation.
*
* @return string
*/
function translationValue($key, $translation)
{
if (array_key_exists($key, $translation) && !empty($translation[$key])) {
return processValue($translation[$key]);
}
return '';
}
/**
* Display the default language and translation.
*
* @param array $default Array containing every default translation.
* @param array $translation Array containing every translation for the
* translation file.
* @param string $prevKey Default ''. Otherwise previous key.
* @param int $px Default 0. Left margin for input fields,
* used for indentation.
*
* @return null
*/
function displayHTML(
$default,
$translation,
$prevKey = '',
$px = 0
) {
global $disable;
//foreach key check if value is an array -> run function again with that array
//else echo input -> key, default, translation
foreach ($default as $key => $value) {
$key = processValue($key);
if (is_array($value)) {
echo "<input style=\"margin:5px 12px 0px " . $px . "px; width:15vw;\"
tabindex=\"-1\" type=\"text\" value=\"$key\" $disable><br>\n\t";
displayHTML($value, $translation[$key], $key, 24);
continue;
}
$value = processValue($value);
echo "<input style=\"margin:5px 12px 0px " . $px . "px; width:15vw;\"
type=\"text\" tabindex=\"-1\" value=\"$key\" $disable>\n\t";
echo "<input style=\"margin:5px 12px 0px 0px;\" type=\"text\"
tabindex=\"-1\" value=\"$value\" $disable>\n\t";
//if key is nested -> name is main|nested
$name = ($prevKey != '') ? $prevKey . "|" . $key : $key;
echo "<input style=\"margin:5px 0px 0px 0px; " .
setStyle($key, $translation, $value) . "\" type=\"text\"
name=\"" . processValue($name) . "\"
value=\"" . translationValue($key, $translation) . "\"
$disable><br>\n\t";
}
}
/**
* Save translation.
* Create array from input.
*
* @todo add option to show output using textarea
* @return string
*/
function saveTranslation()
{
$array = array();
foreach ($_POST as $key => $value) {
if ($key == "submit") {
continue;
}
if (empty($value)) {
continue;
}
$containsSub = strpos($key, '|');
if ($containsSub === false) {
$array[$key] = $value;
continue;
}
$main = substr($key, 0, $containsSub);
$sub = substr($key, $containsSub + 1);
array_key_exists($main, $array)
? $array[$main][$sub] = $value
: $array[$main] = array($sub => $value);
}
$content = createContentForSave($array);
return modifyFile($content);
}
/**
* Change array to string.
* Add license and documentation to content.
* Make content ready to add to the translation file.
*
* @param array $array Contains processed input.
*
* @return string
*/
function createContentForSave($array)
{
$content = "\$sm_lang = array(\n";
foreach ($array as $key => $value) {
if (is_array($value)) {
$content .= str_repeat(' ', 4) . "'" . $key . "' => array(\n";
foreach ($array[$key] as $nestedKey => $nestedValue) {
$content .= str_repeat(' ', 8) . "'$nestedKey' => '" .
wordwrap($nestedValue, (120 - strlen($nestedKey) - 9 - 8), "\n ") . "',\n";
}
$content .= str_repeat(' ', 4) . "),\n";
continue;
}
$content .= str_repeat(' ', 4) . "'" . $key . "' => '$value',\n";
}
$content .= ");\n";
return $content;
}
/**
* Load changes to translation file.
* Copy documentation and past the new translation under it.
*
* @param string $newContent New content for file. Default set to prevent errors.
*
* @return string
*/
function modifyFile($newContent = '')
{
global $path;
global $translationLang;
$currentContent = file_get_contents($path . "/" . $translationLang);
$licenseAndDocs = substr(
$currentContent,
0,
strpos($currentContent, '$sm_lang')
);
file_put_contents($path . "/" . $translationLang, $licenseAndDocs . $newContent);
return "Success";
}
/**
* Check if file is writable. (Currently required to save.)
* Check if $_POST["submit"] isset.
*
* @return string
*/
function checkForSave()
{
global $path;
global $translationLang;
global $disable;
$message = false;
$messageBox = "<div style=\"width:50vw; margin-left:25vw; padding:12px;
border:black 1px solid; border-radius:5px;\">";
if (!is_writable($path . "/" . $translationLang)) {
$message = true;
$messageBox .= "<b style=\"color:red;\">File not writable.</b><br>
Permission: "
. substr(sprintf('%o', fileperms($path . "/" . $translationLang)), -4)
. ".<br>Should be 0666.<br>Unix: chmod 0666 $path/$translationLang";
$disable = "disabled";
}
if (isset($_POST["submit"])) {
$message = true;
$messageBox .= saveTranslation();
}
$messageBox .= "</div><br><br>";
return $message ? $messageBox : '';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>
PSMLE - <?php echo $translationLang; ?>
</title>
<style>
html, body{
overflow-x:hidden;
font-family: system-ui;
margin: 0px 0px 0px 1vw;
}
input{width:30vw;}
ul{list-style-type: circle;}
button{
width: 49vw;
height: 50px;
border-radius: 10px;
background-color: rgb(75, 205, 20);
border: black 1px solid;
color: white;
font-size: 15px;
margin: 24px 0px 24px 24vw;
}
button:active{background-color: rgb(43, 117, 12);}
button:hover{background-color: rgb(61, 166, 16);}
</style>
<!--[if lt IE 9]>
<script src="/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<ul>
<li>Red: no translation found.</li>
<li>Orange: possibly not translated.</li>
</ul>
<br>
<b>
DON'T FORGET TO SAVE!
<br><br>
If you want to change the default translation,
select it as the translation file.
</b>
<br><br>
<?php
echo checkForSave();
?>
<input style="margin:5px 12px 0px 0px; width:15vw;" value="Key"
tabindex="-1" readonly>
<input style="margin:5px 12px 0px 0px;" value="Default"
tabindex="-1" readonly>
<input style="margin:5px 12px 0px 0px;" value="Translation"
tabindex="-1" readonly>
<br><br><br>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
displayHTML($default, $translation);
?>
<button type="submit" name="submit" value="1">Save translation</button>
</form>
</body>
</html> </form>
</body>
</html>