LanguageController.php 13.8 KB
Newer Older
1
2
3
4
5
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
6
use App\Models\LanguageFile;
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
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

class LanguageController extends Controller
{
    public function createOverview(Request $request)
    {
        $languageFilePath = resource_path() . "/lang/";
        $files            = scandir($languageFilePath);
        $dirs             = [];
        foreach ($files as $file) {
            if (is_dir($languageFilePath . $file) && $file !== "." && $file !== "..") {
                $dirs[] = $file;
            }

        }
        # Im Array "$dirs" haben wir nun alle Verzeichnisse mit dem entsprechenden Sprachkürzel
        # Alle von uns bislang unterstützen Sprachen sind hier eingetragen.
        $langTexts = [];
        $sum       = [];
        foreach ($dirs as $dir) {
            # Wir überprüfen nun für jede Datei die Anzahl der vorhandenen Übersetzungen
            $di                           = new RecursiveDirectoryIterator($languageFilePath . $dir);
            $langTexts[$dir]["textCount"] = 0;
            $langTexts[$dir]["fileCount"] = 0;
            foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
                if (!$this->endsWith($filename, ".")) {
                    $langTexts[$dir]["fileCount"] += 1;
                    $tmp = include $filename;
                    foreach ($tmp as $key => $value) {
37
                        $sum = array_merge($sum, $this->getValues([$key => $value], basename($filename)));
38
                        $langTexts[$dir]["textCount"] += count($this->getValues([$key => $value]));
39
                    }
40

41
42
43
44
45
46
47
48
49
50
51
52
                }

            }
        }
        $deComplete = $langTexts["de"]["textCount"] === count($sum) ? true : false;
        return view('languages.overview')
            ->with('title', trans('titles.languages'))
            ->with('langTexts', $langTexts)
            ->with('sum', $sum)
            ->with('deComplete', $deComplete);
    }

53
    public function createEditPage($from, $to, $exclude = "", $email = "")
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    {
        $languageFilePath = resource_path() . "/lang/";
        $files            = scandir($languageFilePath);
        $dirs             = [];
        foreach ($files as $file) {
            if (is_dir($languageFilePath . $file) && $file !== "." && $file !== "..") {
                $dirs[$file] = $file;
            }

        }
        # Abbruchbedingungen:
        if ($from === "" || $to === "" || ($from !== "de" && $from !== "all") || ($from === "all" && $to !== "de") && !array_has($dirs, $to)) {
            return redirect(url('languages'));
        }

        $texts = [];

71
72
73
        $langTexts = [];
        $sum       = [];
        $filePath  = [];
74
75
76
77
78
79
        foreach ($dirs as $dir) {
            if ($from !== "all" && $dir !== $to && $dir !== $from) {
                continue;
            }

            # Wir überprüfen nun für jede Datei die Anzahl der vorhandenen Übersetzungen
80
81
            $di              = new RecursiveDirectoryIterator($languageFilePath . $dir);
            $langTexts[$dir] = 0;
82
83
84
85
            foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
                if (!$this->endsWith($filename, ".")) {
                    $tmp = include $filename;
                    foreach ($tmp as $key => $value) {
86
                        $sum                                    = array_merge($sum, $this->getValues([$key => $value], basename($filename)));
87
                        $texts[basename($filename)][$key][$dir] = $value;
88
                        $langTexts[$dir] += count($this->getValues([$key => $value]));
89
                    }
90
                    $filePath[basename($filename)] = preg_replace("/lang\/.*?\//si", "lang/$to/", substr($filename, strpos($filename, "lang")));
91

92
93
94
95
                }

            }
        }
96
        die();
97
98
        $langs = [];
        $fn    = "";
99
100
101
102
103
104
105
106
107
        $t     = [];
        $ex    = ['files' => [], 'new' => 0];
        if ($exclude !== "") {
            try {
                $ex = unserialize(base64_decode($exclude));
            } catch (\ErrorException $e) {
                $ex = ['files' => [], 'new' => 0];
            }
        }
108

109
110
111
112
113
114
115
116
117
118
        foreach ($texts as $filename => $text) {
            $has = false;
            foreach ($ex['files'] as $file) {
                if ($file === $filename) {
                    $has = true;
                }
            }
            if ($has) {
                continue;
            }
119
120
121
            while ($this->hasToMuchDimensions($text)) {
                $text = $this->deMultidimensionalizeArray($text);
            }
122
            # Hier können wir später die bereits bearbeiteten Dateien ausschließen.
123
124
            foreach ($text as $textname => $languages) {

125
126
127
128
129
                if ($languages === "") {
                    continue;
                }

                $complete = true;
130
131
132
133
                foreach ($languages as $lang => $value) {
                    if ($lang !== $to) {
                        $langs = array_add($langs, $lang, $lang);
                    }
134
135
136
                    if (!isset($languages[$to]) && isset($languages[$lang])) {
                        $complete = false;
                    }
137
138
139

                }
                if (!isset($languages[$to])) {
140
141
                    $fn = $filePath[$filename];
                    $t  = $text;
142
143
144
145
146
147
                    break;
                }
            }

        }

148
        $t = $this->htmlEscape($t, $to);
149
        $t = $this->createHints($t, $to);
150

151
152
153
154
155
        return view('languages.edit')
            ->with('texts', $t)
            ->with('filename', $fn)
            ->with('title', trans('titles.languages.edit'))
            ->with('langs', $langs)
156
157
158
            ->with('to', $to)
            ->with('langTexts', $langTexts)
            ->with('sum', $sum)
159
160
            ->with('new', $ex["new"])
            ->with('email', $email);
161
162
    }

163
    public function createSynopticEditPage(Request $request, $exclude = "") 
164
165
    {
        $languageFilePath = resource_path() . "/lang/";
166
167
168
169
170
171
        $languageFolders  = scandir($languageFilePath); #relativer Pfad
        $languageFiles    = [];
        #Zusammensetzen zu absolutem Pfad
        foreach ($languageFolders as $folder) {
            if (is_dir($languageFilePath . $folder) && $folder !== "." && $folder !== "..") {
                $languageFiles[$folder] = new LanguageFile($languageFilePath.$folder);
172
            }
173
174
        }

175

176
177
        foreach ($languageFiles as $folderName => $languageFile) {
            
178
179
        }

180
        /*
181
182
183
184
        $texts = [];
        $langTexts = [];
        $sum       = [];
        $filePath  = [];
185
186


187
188
189
190
191
        foreach ($dirs as $dir) {
            # Wir überprüfen nun für jede Datei die Anzahl der vorhandenen Übersetzungen
            $di              = new RecursiveDirectoryIterator($languageFilePath . $dir);
            $langTexts[$dir] = 0;
            foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
192
                //var_dump($filename." - ".$file."<br>");
193
194
195
                if (!$this->endsWith($filename, ".")) {
                    $tmp = include $filename;
                    foreach ($tmp as $key => $value) {
196
                        var_dump($key." - ".$value."<br>");
197
198
199
200
201
202
                        $sum                                    = array_merge($sum, $this->getValues([$key => $value], basename($filename)));
                        $texts[basename($filename)][$key][$dir] = $value;
                        $langTexts[$dir] += count($this->getValues([$key => $value]));
                    }
                    //$filePath[basename($filename)] = preg_replace("/lang\/.*?\//si", "lang/$to/", substr($filename, strpos($filename, "lang")));
                }
203
204
205
            }
        }*/
        die();
206

207
208
209
210
211
       /* foreach($texts as $a => $text) {
            foreach($text as $b => $k) {
                foreach($k as $c => $d) {
                    die(var_dump($a." - ".$b." - ".$c." - ".$d));
                }
212
213
            }
        }
214
215
216
        */
        /*
        $langs = [];
217
218
219
220
221
222
223
224
225
226
        $fn    = "";
        $t     = [];
        $ex    = ['files' => [], 'new' => 0];
        if ($exclude !== "") {
            try {
                $ex = unserialize(base64_decode($exclude));
            } catch (\ErrorException $e) {
                $ex = ['files' => [], 'new' => 0];
            }
        }
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
        foreach ($texts as $filename => $text) {
            $has = false;
            foreach ($ex['files'] as $file) {
                if ($file === $filename) {
                    $has = true;
                }
            }
            if ($has) {
                continue;
            }
            while ($this->hasToMuchDimensions($text)) {
                $text = $this->deMultidimensionalizeArray($text);
            }
            # Hier können wir später die bereits bearbeiteten Dateien ausschließen.
            foreach ($text as $textname => $languages) {

                if ($languages === "") {
                    continue;
                }

                $complete = true;
                foreach ($languages as $lang => $value) {
                    if ($lang !== $to) {
                        $langs = array_add($langs, $lang, $lang);
                    }
                    if (!isset($languages[$to]) && isset($languages[$lang])) {
                        $complete = false;
                    }

                }
                if (!isset($languages[$to])) {
                    $fn = $filePath[$filename];
                    $t  = $text;
                    break;
                }
            }

        }

        $t = $this->htmlEscape($t, $to);
        $t = $this->createHints($t, $to);
269
    */
270

271
        return view('languages.synoptic');
272
273
    }

274
275
276
277
278
279
280
281
282
283
284
285
286
287
    private function htmlEscape($t, $to)
    {
        foreach ($t as $key => $langTexts) {
            if ($langTexts !== "") {
                foreach ($langTexts as $lang => $text) {
                    if ($lang !== $to) {
                        $t[$key][$lang] = htmlspecialchars($text);
                    }
                }
            }
        }
        return $t;
    }

288
289
290
    private function createHints($t, $to)
    {
        foreach ($t as $key => $langTexts) {
291
292
293
            if ($langTexts !== "") {
                foreach ($langTexts as $lang => $text) {
                    if ($lang !== $to) {
294
295
                        if (preg_match("/:\w+/si", $text)) {
                            $t[$key][$lang] = preg_replace("/(:\w+)/si", "<a class=\"text-danger hint\" data-toggle=\"tooltip\" data-trigger=\"hover\" data-placement=\"auto\" title=\"Dies ist ein Variablenname. Er wird dort, wo der Text verwendet wird durch einen dynamischen Wert ersetzt. In der Übersetzung sollte dieser deshalb auch so wie er ist in den Satz integriert werden.\" data-container=\"body\" >$1</a>", $text);
296
                        }
297
298
299
                        if (preg_match("/&lt;.*?&gt;/si", $text)) {
                            $t[$key][$lang] = preg_replace("/(&lt;.*?&gt;)/si", "<a class=\"text-danger hint\" data-toggle=\"tooltip\" data-trigger=\"hover\" data-placement=\"auto\" title=\"Dies ist ein sogenanntes HTML-Tag. Wenn Sie sich das zutrauen, bauen Sie diese HTML Tags gerne so wie sie sind in Ihre Übersetzung ein. Achten Sie hierbei darauf, dass der Text zwischen den Tags auch bei der Übersetzung an der logisch gleichen Stelle von den Tags umfasst ist.\" data-container=\"body\" >$1</a>", $text);
                        }
300

301
                    }
302
303
304
305
306
307
                }
            }
        }
        return $t;
    }

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
    private function getValues($values, $prefix = "")
    {
        $return = [];
        if (!is_array($values)) {
            return $return;
        } else {
            foreach ($values as $key => $value) {
                if (is_array($value)) {
                    $return = array_merge($return, $this->getValues($value, $prefix . $key));
                } elseif (is_string($value)) {
                    $return[$prefix . $key] = $value;
                }
            }
        }

        return $return;
    }

    private function hasToMuchDimensions($t)
    {
        foreach ($t as $key => $value) {
            if (is_array($value)) {
                foreach ($value as $lang => $val) {
                    if (is_array($val)) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    private function deMultidimensionalizeArray($t)
    {
        # Es gibt noch Besonderheiten in den Texten
        # Es kann sein, dass statt einem String ein Array aus Strings als Werte existieren.
        # Diese müssen aufgelöst werden:
        $tmp = [];
        foreach ($t as $key => $value) {
            $isArray = false;
            if (is_array($value)) {
                foreach ($value as $lang => $val) {
                    if (is_array($val)) {
                        $isArray = true;
                    }

                }
            } else {
                $tmp[$key] = $value;
                continue;
            }
            if (!$isArray) {
                $tmp[$key] = $value;
            } else {
                $tmp[$key] = "";
                foreach ($value as $lang => $val) {
                    if (is_array($val)) {
                        foreach ($val as $key2 => $val) {
                            $tmp["\t" . $key . "#" . $key2][$lang] = $val;
                        }

                    }
                }
            }

        }
        return $tmp;
376
377
    }

378
    public function startsWith($haystack, $needle)
379
380
381
382
383
    {
        // search backwards starting from haystack length characters from the end
        return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false;
    }

384
    public function endsWith($haystack, $needle)
385
386
387
388
389
    {
        // search forward starting from end minus needle length characters
        return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
    }
}