1717use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
1818use Symfony \Component \Intl \Intl ;
1919use Symfony \Component \Translation \MessageCatalogue ;
20- use Symfony \Component \Translation \Translator ;
2120use Translation \Bundle \Exception \MessageValidationException ;
2221use Translation \Bundle \Model \WebUiMessage ;
22+ use Translation \Bundle \Service \StorageService ;
2323use Translation \Common \Exception \StorageException ;
2424use Translation \Bundle \Model \CatalogueMessage ;
25+ use Translation \Common \Model \Message ;
2526
2627/**
2728 * @author Tobias Nyholm <tobias.nyholm@gmail.com>
2829 */
2930class WebUIController extends Controller
3031{
32+ /**
33+ * Show a dashboard for the configuration.
34+ *
35+ * @param string|null $configName
36+ *
37+ * @return Response
38+ */
3139 public function indexAction ($ configName = null )
3240 {
3341 $ config = $ this ->getConfiguration ($ configName );
42+ $ localeMap = $ this ->getLocale2LanguageMap ();
43+ $ catalogues = $ this ->get ('php_translation.catalogue_fetcher ' )->getCatalogues (array_keys ($ localeMap ), [$ config ['output_dir ' ]]);
3444
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 ' ]]);
4245 $ catalogueSize = [];
4346 $ maxDomainSize = [];
4447 $ maxCatalogueSize = 1 ;
48+
49+ // For each catalogue (or locale)
4550 /** @var MessageCatalogue $catalogue */
4651 foreach ($ catalogues as $ catalogue ) {
4752 $ locale = $ catalogue ->getLocale ();
@@ -66,26 +71,29 @@ public function indexAction($configName = null)
6671 'catalogueSize ' => $ catalogueSize ,
6772 'maxDomainSize ' => $ maxDomainSize ,
6873 'maxCatalogueSize ' => $ maxCatalogueSize ,
69- 'locales ' => $ locales ,
74+ 'localeMap ' => $ localeMap ,
7075 'configName ' => $ configName ,
7176 'configNames ' => $ this ->get ('php_translation.configuration_manager ' )->getNames (),
7277 ]);
7378 }
7479
7580 /**
76- * @param $locale
77- * @param $domain
81+ * Show a catalogue.
82+ *
83+ * @param string $configName
84+ * @param string $locale
85+ * @param string $domain
7886 *
7987 * @return Response
8088 */
8189 public function showAction ($ configName , $ locale , $ domain )
8290 {
8391 $ config = $ this ->getConfiguration ($ configName );
8492 $ locales = $ this ->getParameter ('php_translation.locales ' );
85- /** @var Translator $translator */
86- $ catalogues = $ this -> get ( ' php_translation.catalogue_fetcher ' )-> getCatalogues ( $ locales , [ $ config [ ' output_dir ' ]]);
93+
94+ // Get a catalogue manager and load it with all the catalogues
8795 $ catalogueManager = $ this ->get ('php_translation.catalogue_manager ' );
88- $ catalogueManager ->load ($ catalogues );
96+ $ catalogueManager ->load ($ this -> get ( ' php_translation.catalogue_fetcher ' )-> getCatalogues ( $ locales , [ $ config [ ' output_dir ' ]]) );
8997
9098 /** @var CatalogueMessage[] $messages */
9199 $ messages = $ catalogueManager ->getMessages ($ locale , $ domain );
@@ -106,21 +114,24 @@ public function showAction($configName, $locale, $domain)
106114
107115 /**
108116 * @param Request $request
117+ * @param string $configName
118+ * @param string $locale
109119 * @param string $domain
110120 *
111121 * @return Response
112122 */
113123 public function createAction (Request $ request , $ configName , $ locale , $ domain )
114124 {
115- $ storage = $ this ->get ('php_translation.storage.file. ' .$ configName );
125+ /** @var StorageService $storage */
126+ $ storage = $ this ->get ('php_translation.storage. ' .$ configName );
116127 try {
117128 $ message = $ this ->getMessage ($ request , ['Create ' ]);
118129 } catch (MessageValidationException $ e ) {
119130 return new Response ($ e ->getMessage (), 400 );
120131 }
121132
122133 try {
123- $ storage ->set ( $ locale , $ domain , $ message ->getKey (), $ message ->getMessage ());
134+ $ storage ->create ( new Message ( $ message ->getKey (), $ domain , $ locale , $ message ->getMessage () ));
124135 } catch (StorageException $ e ) {
125136 throw new BadRequestHttpException (sprintf (
126137 'Key "%s" does already exist for "%s" on domain "%s". ' ,
@@ -130,7 +141,9 @@ public function createAction(Request $request, $configName, $locale, $domain)
130141 ), $ e );
131142 }
132143
133- return new Response ('Translation created ' );
144+ return $ this ->render ('TranslationBundle:WebUI:create.html.twig ' , [
145+ 'message ' => $ message ,
146+ ]);
134147 }
135148
136149 /**
@@ -149,7 +162,9 @@ public function editAction(Request $request, $configName, $locale, $domain)
149162 return new Response ($ e ->getMessage (), 400 );
150163 }
151164
152- $ this ->get ('php_translation.storage.file. ' .$ configName )->update ($ locale , $ domain , $ message ->getKey (), $ message ->getMessage ());
165+ /** @var StorageService $storage */
166+ $ storage = $ this ->get ('php_translation.storage. ' .$ configName );
167+ $ storage ->update (new Message ($ message ->getKey (), $ domain , $ locale , $ message ->getMessage ()));
153168
154169 return new Response ('Translation updated ' );
155170 }
@@ -170,7 +185,9 @@ public function deleteAction(Request $request, $configName, $locale, $domain)
170185 return new Response ($ e ->getMessage (), 400 );
171186 }
172187
173- $ this ->get ('php_translation.storage.file. ' .$ configName )->delete ($ locale , $ domain , $ message ->getKey ());
188+ /** @var StorageService $storage */
189+ $ storage = $ this ->get ('php_translation.storage. ' .$ configName );
190+ $ storage ->delete ($ locale , $ domain , $ message ->getKey ());
174191
175192 return new Response ('Message was deleted ' );
176193 }
@@ -222,4 +239,21 @@ private function getMessage(Request $request, array $validationGroups = [])
222239
223240 return $ message ;
224241 }
242+
243+ /**
244+ * This will return a map of our configured locales and their language name.
245+ *
246+ * @return array locale => language
247+ */
248+ private function getLocale2LanguageMap ()
249+ {
250+ $ configuedLocales = $ this ->getParameter ('php_translation.locales ' );
251+ $ names = Intl::getLocaleBundle ()->getLocaleNames ('en ' );
252+ $ map = [];
253+ foreach ($ configuedLocales as $ l ) {
254+ $ map [$ l ] = $ names [$ l ];
255+ }
256+
257+ return $ map ;
258+ }
225259}
0 commit comments