1717namespace Pimcore \Bundle \StudioBackendBundle \Translation \Repository ;
1818
1919use Pimcore \Bundle \StaticResolverBundle \Lib \Tools \AdminResolverInterface ;
20+ use Pimcore \Bundle \StudioBackendBundle \Exception \Api \ElementExistsException ;
2021use Pimcore \Bundle \StudioBackendBundle \Exception \Api \InvalidLocaleException ;
2122use Pimcore \Bundle \StudioBackendBundle \Exception \Api \NotFoundException ;
2223use Pimcore \Bundle \StudioBackendBundle \Translation \Schema \TranslationData ;
2324use Pimcore \Bundle \StudioBackendBundle \Translation \Service \TranslatorServiceInterface ;
2425use Pimcore \Model \Translation ;
2526use Pimcore \Model \Translation \Listing ;
2627use function in_array ;
28+ use function sprintf ;
2729
2830/**
2931 * @internal
@@ -53,23 +55,48 @@ public function getAllTranslations(string $locale): array
5355 /**
5456 * {@inheritdoc}
5557 */
56- public function createTranslations (array $ translationData , string $ locale ): void
58+ public function createTranslations (string $ key , string $ type ): void
59+ {
60+ $ languages = $ this ->adminResolver ->getLanguages ();
61+
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 ();
76+
77+ }
78+
79+ /**
80+ * {@inheritdoc}
81+ */
82+ public function updateTranslations (array $ translationData , string $ locale ): void
5783 {
5884 $ languages = $ this ->adminResolver ->getLanguages ();
5985 $ this ->validateLocale ($ locale , $ languages );
6086
6187 /** @var TranslationData $translation */
6288 foreach ($ translationData as $ translation ) {
89+ if ($ this ->getTranslationByKey ($ translation ->getKey ()) === null ) {
90+ throw new NotFoundException ('translation ' , $ translation ->getKey (), 'key ' );
91+ }
92+
6393 $ t = new Translation ();
6494 $ t ->setDomain (TranslatorServiceInterface::DOMAIN );
6595 $ t ->setKey ($ translation ->getKey ());
6696 $ t ->setType ($ translation ->getType ());
6797 $ t ->addTranslation ($ locale , $ translation ->getTranslation ());
6898 $ t ->setCreationDate (time ());
6999 $ t ->setModificationDate (time ());
70- if ($ this ->getTranslationByKey ($ translation ->getKey ()) === null ) {
71- $ this ->setNewValues ($ t , $ languages , $ locale );
72- }
73100 $ t ->save ();
74101 }
75102 }
@@ -94,14 +121,10 @@ private function getTranslationList(): Listing
94121 return $ list ;
95122 }
96123
97- private function setNewValues (Translation $ translation , array $ languages, string $ locale ): void
124+ private function setNewValues (Translation $ translation , array $ languages ): void
98125 {
99- $ translation ->setCreationDate (time ());
100-
101126 foreach ($ languages as $ language ) {
102- if ($ language !== $ locale ) {
103- $ translation ->addTranslation ($ language , '' );
104- }
127+ $ translation ->addTranslation ($ language , '' );
105128 }
106129 }
107130
0 commit comments