1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Translation\Bundle\Controller; |
13
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
18
|
|
|
use Symfony\Component\Intl\Intl; |
19
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
20
|
|
|
use Symfony\Component\Translation\Translator; |
21
|
|
|
use Translation\Bundle\Exception\MessageValidationException; |
22
|
|
|
use Translation\Bundle\Model\GuiMessageRepresentation; |
23
|
|
|
use Translation\Common\Exception\StorageException; |
24
|
|
|
use Translation\Symfony\Model\Message; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Tobias Nyholm <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class WebUIController extends Controller |
30
|
|
|
{ |
31
|
|
|
public function indexAction($configName = null) |
32
|
|
|
{ |
33
|
|
|
$config = $this->getConfiguration($configName); |
34
|
|
|
|
35
|
|
|
$configuedLocales = $this->getParameter('php_translation.locales'); |
36
|
|
|
$allLocales = Intl::getLocaleBundle()->getLocaleNames('en'); |
37
|
|
|
$locales = []; |
38
|
|
|
foreach ($configuedLocales as $l) { |
39
|
|
|
$locales[$l] = $allLocales[$l]; |
40
|
|
|
} |
41
|
|
|
$catalogues = $this->get('php_translation.catalogue_fetcher')->getCatalogues($configuedLocales, [$config['output_dir']]); |
42
|
|
|
$catalogueSize = []; |
43
|
|
|
$maxDomainSize = []; |
44
|
|
|
$maxCatalogueSize = 1; |
45
|
|
|
/** @var MessageCatalogue $catalogue */ |
46
|
|
|
foreach ($catalogues as $catalogue) { |
47
|
|
|
$locale = $catalogue->getLocale(); |
48
|
|
|
$domains = $catalogue->all(); |
49
|
|
|
ksort($domains); |
50
|
|
|
$catalogueSize[$locale] = 0; |
51
|
|
|
foreach ($domains as $domain => $messages) { |
52
|
|
|
$count = count($messages); |
53
|
|
|
$catalogueSize[$locale] += $count; |
54
|
|
|
if (!isset($maxDomainSize[$domain]) || $count > $maxDomainSize[$domain]) { |
55
|
|
|
$maxDomainSize[$domain] = $count; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($catalogueSize[$locale] > $maxCatalogueSize) { |
60
|
|
|
$maxCatalogueSize = $catalogueSize[$locale]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->render('TranslationBundle:WebUI:index.html.twig', [ |
65
|
|
|
'catalogues' => $catalogues, |
66
|
|
|
'catalogueSize' => $catalogueSize, |
67
|
|
|
'maxDomainSize' => $maxDomainSize, |
68
|
|
|
'maxCatalogueSize' => $maxCatalogueSize, |
69
|
|
|
'locales' => $locales, |
70
|
|
|
'configName' => $configName, |
71
|
|
|
'configNames' => $this->get('php_translation.configuration_manager')->getNames(), |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $locale |
77
|
|
|
* @param $domain |
78
|
|
|
* |
79
|
|
|
* @return Response |
80
|
|
|
*/ |
81
|
|
|
public function showAction($configName, $locale, $domain) |
82
|
|
|
{ |
83
|
|
|
$config = $this->getConfiguration($configName); |
84
|
|
|
$locales = $this->getParameter('php_translation.locales'); |
85
|
|
|
/** @var Translator $translator */ |
86
|
|
|
$catalogues = $this->get('php_translation.catalogue_fetcher')->getCatalogues($locales, [$config['output_dir']]); |
87
|
|
|
$catalogueManager = $this->get('php_translation.catalogue_manager'); |
88
|
|
|
$catalogueManager->load($catalogues); |
89
|
|
|
|
90
|
|
|
/** @var Message[] $messages */ |
91
|
|
|
$messages = $catalogueManager->getMessages($locale, $domain); |
92
|
|
|
usort($messages, function (Message $a, Message $b) { |
93
|
|
|
return strcmp($a->getKey(), $b->getKey()); |
94
|
|
|
}); |
95
|
|
|
|
96
|
|
|
return $this->render('TranslationBundle:WebUI:show.html.twig', [ |
97
|
|
|
'messages' => $messages, |
98
|
|
|
'domains' => $catalogueManager->getDomains(), |
99
|
|
|
'currentDomain' => $domain, |
100
|
|
|
'locales' => $locales, |
101
|
|
|
'currentLocale' => $locale, |
102
|
|
|
'configName' => $configName, |
103
|
|
|
'configNames' => $this->get('php_translation.configuration_manager')->getNames(), |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param Request $request |
109
|
|
|
* @param string $domain |
110
|
|
|
* |
111
|
|
|
* @return Response |
112
|
|
|
*/ |
113
|
|
|
public function createAction(Request $request, $configName, $locale, $domain) |
114
|
|
|
{ |
115
|
|
|
$storage = $this->get('php_translation.storage.file.'.$configName); |
116
|
|
|
try { |
117
|
|
|
$message = $this->getMessage($request, ['Create']); |
118
|
|
|
} catch (MessageValidationException $e) { |
119
|
|
|
return new Response($e->getMessage(), 400); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
try { |
123
|
|
|
$storage->set($locale, $domain, $message->getKey(), $message->getMessage()); |
124
|
|
|
} catch (StorageException $e) { |
125
|
|
|
throw new BadRequestHttpException(sprintf( |
126
|
|
|
'Key "%s" does already exist for "%s" on domain "%s".', |
127
|
|
|
$message->getKey(), |
128
|
|
|
$locale, |
129
|
|
|
$domain |
130
|
|
|
), $e); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return new Response('Translation created'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Request $request |
138
|
|
|
* @param string $configName |
139
|
|
|
* @param string $locale |
140
|
|
|
* @param string $domain |
141
|
|
|
* |
142
|
|
|
* @return Response |
143
|
|
|
*/ |
144
|
|
View Code Duplication |
public function editAction(Request $request, $configName, $locale, $domain) |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
try { |
147
|
|
|
$message = $this->getMessage($request, ['Edit']); |
148
|
|
|
} catch (MessageValidationException $e) { |
149
|
|
|
return new Response($e->getMessage(), 400); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->get('php_translation.storage.file.'.$configName)->update($locale, $domain, $message->getKey(), $message->getMessage()); |
153
|
|
|
|
154
|
|
|
return new Response('Translation updated'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param Request $request |
159
|
|
|
* @param string $configName |
160
|
|
|
* @param string $locale |
161
|
|
|
* @param string $domain |
162
|
|
|
* |
163
|
|
|
* @return Response |
164
|
|
|
*/ |
165
|
|
View Code Duplication |
public function deleteAction(Request $request, $configName, $locale, $domain) |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
try { |
168
|
|
|
$message = $this->getMessage($request, ['Delete']); |
169
|
|
|
} catch (MessageValidationException $e) { |
170
|
|
|
return new Response($e->getMessage(), 400); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->get('php_translation.storage.file.'.$configName)->delete($locale, $domain, $message->getKey()); |
174
|
|
|
|
175
|
|
|
return new Response('Message was deleted'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param $configName |
180
|
|
|
* |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
|
|
private function getConfiguration(&$configName) |
184
|
|
|
{ |
185
|
|
|
$configurationManager = $this->get('php_translation.configuration_manager'); |
186
|
|
|
$configName = $configName !== null ? $configName : $configurationManager->getFirstName(); |
187
|
|
|
if ($configName === null) { |
188
|
|
|
throw new \LogicException('You must configure at least one key under translation.config'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$config = $configurationManager->getConfiguration($configName); |
192
|
|
|
|
193
|
|
|
if (empty($config)) { |
194
|
|
|
throw $this->createNotFoundException('No translation configuration named "'.$configName.'" was found.'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $config; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param Request $request |
202
|
|
|
* @param array $validationGroups |
203
|
|
|
* |
204
|
|
|
* @return GuiMessageRepresentation |
205
|
|
|
*/ |
206
|
|
|
private function getMessage(Request $request, array $validationGroups = []) |
207
|
|
|
{ |
208
|
|
|
$json = $request->getContent(); |
209
|
|
|
$data = json_decode($json, true); |
210
|
|
|
$message = new GuiMessageRepresentation(); |
211
|
|
|
if (isset($data['key'])) { |
212
|
|
|
$message->setKey($data['key']); |
213
|
|
|
} |
214
|
|
|
if (isset($data['message'])) { |
215
|
|
|
$message->setMessage($data['message']); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$errors = $this->get('validator')->validate($message, null, $validationGroups); |
219
|
|
|
if (count($errors) > 0) { |
220
|
|
|
throw MessageValidationException::create(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $message; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.