1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Terranet\Administrator\Events\TranslationsChanged; |
8
|
|
|
use Terranet\Administrator\Services\Translator; |
9
|
|
|
|
10
|
|
|
ini_set('opcache.enable', 0); |
11
|
|
|
|
12
|
|
|
class TranslationsController extends AdminController |
13
|
|
|
{ |
14
|
|
|
/** @var Translator */ |
15
|
|
|
protected $translator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return Translator |
19
|
|
|
*/ |
20
|
|
|
protected function translator(): ?Translator |
21
|
|
|
{ |
22
|
|
|
if (null === $this->translator) { |
23
|
|
|
$this->translator = app(Translator::class)->setLocales( |
|
|
|
|
24
|
|
|
$this->locales() |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return $this->translator; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function index(Request $request) |
32
|
|
|
{ |
33
|
|
|
$page = $request->get('page', 1); |
34
|
|
|
$term = $request->get('term'); |
35
|
|
|
$only = $request->get('only'); |
36
|
|
|
|
37
|
|
|
$pagination = $this->translator()->load( |
38
|
|
|
$term, |
39
|
|
|
'all' === $only ? null : $only, |
40
|
|
|
$page, |
41
|
|
|
20 |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$pagination->appends(compact('term', 'only')); |
45
|
|
|
|
46
|
|
|
return view(app('scaffold.template')->translations('index'), [ |
|
|
|
|
47
|
|
|
'pagination' => $pagination, |
48
|
|
|
'scopes' => $this->translator()->filters(), |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function store(Request $request) |
53
|
|
|
{ |
54
|
|
|
$redirectTo = redirect()->back()->with('messages', [trans('administrator::messages.update_success')]); |
|
|
|
|
55
|
|
|
|
56
|
|
|
if (empty($translation = $request->input('translation'))) { |
57
|
|
|
return $redirectTo; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$changed = []; |
61
|
|
|
foreach ($this->locales() as $locale) { |
62
|
|
|
// protect against saving foreign languages |
63
|
|
|
if (app('scaffold.translations')->readonly($locale)) { |
|
|
|
|
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$changed[] = $locale->iso6391(); |
68
|
|
|
$this->translator()->save($translation, $locale->iso6391()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
event(new TranslationsChanged($changed)); |
|
|
|
|
72
|
|
|
|
73
|
|
|
return $redirectTo; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return Collection |
78
|
|
|
*/ |
79
|
|
|
protected function locales(): Collection |
80
|
|
|
{ |
81
|
|
|
return \localizer\locales(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|