|
| 1 | +<?php |
| 2 | + |
| 3 | +use Kirby\Data\Json; |
| 4 | +use Kirby\Filesystem\Dir; |
| 5 | +use Kirby\Filesystem\F; |
| 6 | +use Kirby\Http\Response; |
| 7 | + |
| 8 | +$contentRoot = kirby()->root('content'); |
| 9 | +$path = $contentRoot.DS.'localizer'; |
| 10 | +$localisations = $contentRoot.DS.'localizer'.DS."localisations.json"; |
| 11 | + |
| 12 | +return [ |
| 13 | + 'routes' => [ |
| 14 | + [ |
| 15 | + 'pattern' => '/localizer/translations', |
| 16 | + 'method' => 'get', |
| 17 | + 'action' => function () use ($path) { |
| 18 | + try { |
| 19 | + $files = Dir::read($path); |
| 20 | + $data = []; |
| 21 | + |
| 22 | + foreach ($files as $file) { |
| 23 | + $content = F::read($path.DS.$file); |
| 24 | + if (F::is($file, 'json')) { |
| 25 | + $data[] = Json::decode($content); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + return Response::json($data); |
| 30 | + } catch (Exception $exception) { |
| 31 | + return Response::json([]); |
| 32 | + } |
| 33 | + }, |
| 34 | + ], |
| 35 | + [ |
| 36 | + 'pattern' => '/localizer/translations', |
| 37 | + 'method' => 'post', |
| 38 | + 'action' => function () use ($path) { |
| 39 | + $translations = kirby()->request()->body()->data(); |
| 40 | + $languages = []; |
| 41 | + |
| 42 | + foreach ($translations as $translation) { |
| 43 | + $id = $translation['language']['id']; |
| 44 | + Json::write($path.DS."$id.json", $translation); |
| 45 | + |
| 46 | + // save language id for deletion check |
| 47 | + $languages[] = $id; |
| 48 | + } |
| 49 | + |
| 50 | + // delete removed languages groups |
| 51 | + $files = Dir::read($path); |
| 52 | + foreach ($files as $file) { |
| 53 | + $filename = F::name($file); |
| 54 | + if (!in_array($filename, $languages)) { |
| 55 | + F::remove($path.DS.$file); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return Response::json($translations); |
| 60 | + }, |
| 61 | + ], |
| 62 | + ], |
| 63 | +]; |
0 commit comments