2020use Pimcore \Bundle \StudioBackendBundle \Exception \Api \ElementExistsException ;
2121use Pimcore \Bundle \StudioBackendBundle \Exception \Api \InvalidLocaleException ;
2222use Pimcore \Bundle \StudioBackendBundle \Exception \Api \NotFoundException ;
23+ use Pimcore \Bundle \StudioBackendBundle \Translation \Schema \CreateTranslationData ;
2324use Pimcore \Bundle \StudioBackendBundle \Translation \Schema \TranslationData ;
2425use Pimcore \Bundle \StudioBackendBundle \Translation \Service \TranslatorServiceInterface ;
2526use Pimcore \Model \Translation ;
@@ -55,25 +56,20 @@ public function getAllTranslations(string $locale): array
5556 /**
5657 * {@inheritdoc}
5758 */
58- public function createTranslations (string $ key , string $ type ): void
59+ public function createTranslations (array $ translationData ): void
5960 {
6061 $ languages = $ this ->adminResolver ->getLanguages ();
6162
62- if ($ this ->getTranslationByKey ($ key ) !== null ) {
63- throw new ElementExistsException (
64- sprintf ("Translation with key '%s' already exists " , $ key ),
65- );
66- }
67-
68- $ t = new Translation ();
69- $ t ->setDomain (TranslatorServiceInterface::DOMAIN );
70- $ t ->setKey ($ key );
71- $ t ->setType ($ type );
72- $ t ->setCreationDate (time ());
73- $ t ->setModificationDate (time ());
74- $ this ->setNewValues ($ t , $ languages );
75- $ t ->save ();
63+ /** @var CreateTranslationData $translation */
64+ foreach ($ translationData as $ translation ) {
65+ if ($ this ->getTranslationByKey ($ translation ->getKey ()) !== null ) {
66+ throw new ElementExistsException (
67+ sprintf ("Translation with key '%s' already exists " , $ translation ->getKey ()),
68+ );
69+ }
7670
71+ $ this ->createTranslationEntry ($ translation ->getKey (), $ translation ->getType (), $ languages );
72+ }
7773 }
7874
7975 /**
@@ -86,18 +82,15 @@ public function updateTranslations(array $translationData, string $locale): void
8682
8783 /** @var TranslationData $translation */
8884 foreach ($ translationData as $ translation ) {
89- if ($ this ->getTranslationByKey ($ translation ->getKey ()) === null ) {
85+ $ entry = $ this ->getTranslationByKey ($ translation ->getKey ());
86+ if ($ entry === null ) {
9087 throw new NotFoundException ('translation ' , $ translation ->getKey (), 'key ' );
9188 }
9289
93- $ t = new Translation ();
94- $ t ->setDomain (TranslatorServiceInterface::DOMAIN );
95- $ t ->setKey ($ translation ->getKey ());
96- $ t ->setType ($ translation ->getType ());
97- $ t ->addTranslation ($ locale , $ translation ->getTranslation ());
98- $ t ->setCreationDate (time ());
99- $ t ->setModificationDate (time ());
100- $ t ->save ();
90+ $ entry ->addTranslation ($ locale , $ translation ->getTranslation ());
91+ $ entry ->setType ($ translation ->getType ());
92+ $ entry ->setModificationDate (time ());
93+ $ entry ->save ();
10194 }
10295 }
10396
@@ -111,6 +104,18 @@ public function deleteTranslation(string $key): void
111104 $ translation ->delete ();
112105 }
113106
107+ private function createTranslationEntry (string $ key , string $ type , array $ languages ): void
108+ {
109+ $ t = new Translation ();
110+ $ t ->setDomain (TranslatorServiceInterface::DOMAIN );
111+ $ t ->setKey ($ key );
112+ $ t ->setType ($ type );
113+ $ t ->setCreationDate (time ());
114+ $ t ->setModificationDate (time ());
115+ $ this ->setNewValues ($ t , $ languages );
116+ $ t ->save ();
117+ }
118+
114119 private function getTranslationList (): Listing
115120 {
116121 $ list = new Translation \Listing ();
0 commit comments