1414use Pimcore \Controller \FrontendController ;
1515use Symfony \Component \HttpFoundation \JsonResponse ;
1616use Pimcore \Model \WebsiteSetting ;
17+ use Symfony \Component \HttpFoundation \Request ;
1718
1819class DefaultController extends FrontendController
1920{
21+ private $ authKey ;
22+
23+ private $ type ;
24+
25+ private $ domain ;
26+
27+ public function __construct ()
28+ {
29+ $ this ->authKey = WebsiteSetting::getByName ("deepl_auth_key " ) ? WebsiteSetting::getByName ("deepl_auth_key " )->getData () : null ;
30+ $ this ->type = WebsiteSetting::getByName ("deepl_type " ) ? WebsiteSetting::getByName ("deepl_type " )->getData () : null ;
31+ if ($ this ->type == 'PRO ' ) {
32+ $ this ->domain = 'https://api.deepl.com ' ;
33+ } else {
34+ $ this ->domain = 'https://api-free.deepl.com ' ;
35+ }
36+ }
2037
2138 /* used to check if deepl authentication key exists */
2239 public function getAuthKeyAction ()
2340 {
24- $ authKey = WebsiteSetting::getByName ("deepl_auth_key " ) ? WebsiteSetting::getByName ("deepl_auth_key " )->getData () : null ;
25- $ type = WebsiteSetting::getByName ("deepl_type " ) ? WebsiteSetting::getByName ("deepl_type " )->getData () : null ;
26-
27- return new JsonResponse ([
28- "authKey " => $ authKey ,
29- "exists " => (($ authKey == null || "" ) ? false : true ),
30- "type " => $ type ,
31- "type_exists " => (($ type == null || "" ) ? false : true ),
41+ return JsonResponse::create ([
42+ "authKey " => $ this ->authKey ,
43+ "exists " => (($ this ->authKey == null || "" ) ? false : true ),
44+ "type " => $ this ->type ,
45+ "type_exists " => (($ this ->type == null || "" ) ? false : true ),
3246 ]);
3347 }
3448
3549 public function getGlossariesAction ()
3650 {
37- $ authKey = WebsiteSetting::getByName ("deepl_auth_key " ) ? WebsiteSetting::getByName ("deepl_auth_key " )->getData () : null ;
38- $ type = WebsiteSetting::getByName ("deepl_type " ) ? WebsiteSetting::getByName ("deepl_type " )->getData () : null ;
39-
4051 $ glossaries = null ;
4152
42- if ($ authKey ) {
53+ if ($ this -> authKey ) {
4354 $ headers = [
44- 'Authorization ' => 'DeepL-Auth-Key ' . $ authKey ,
55+ 'Authorization ' => 'DeepL-Auth-Key ' . $ this -> authKey ,
4556 ];
4657
4758 $ client = new Client ([
4859 'headers ' => $ headers
4960 ]);
5061
51- if ($ type == 'PRO ' ) {
52- $ response = $ client ->request ('GET ' , 'https://api.deepl.com/v2/glossaries ' );
53- } else {
54- $ response = $ client ->request ('GET ' , 'https://api-free.deepl.com/v2/glossaries ' );
55- }
62+
63+ $ response = $ client ->request ('GET ' , $ this ->domain . '/v2/glossaries ' );
5664
5765 if ($ response ->getStatusCode () == 200 ) {
5866
@@ -66,10 +74,52 @@ public function getGlossariesAction()
6674
6775 }
6876
69- return new JsonResponse ([
77+ return JsonResponse:: create ([
7078 "glossaries " => $ glossaries
7179 ]);
7280 }
7381
82+ public function translate (Request $ request ): JsonResponse
83+ {
84+ $ payload = json_decode ($ request ->getContent (), true );
85+
86+ $ url = $ this ->domain . '/v2/translate ' ;
87+
88+ $ body = [
89+ 'text ' => [$ payload ['text ' ]] ?? [],
90+ 'target_lang ' => $ payload ['target_lang ' ] ?? null ,
91+ 'source_lang ' => $ payload ['source_lang ' ] ?? null ,
92+ 'split_sentences ' => $ payload ['split_sentences ' ] ?? 'nonewlines ' ,
93+ 'tag_handling ' => $ payload ['tag_handling ' ] ?? 'xml ' ,
94+ 'glossary_id ' => $ payload ['glossary_id ' ] ?? null ,
95+ ];
96+
97+ try {
98+ $ client = new Client ();
99+
100+ $ response = $ client ->request ('POST ' , $ url , [
101+ 'headers ' => [
102+ 'Authorization ' => 'DeepL-Auth-Key ' . $ this ->authKey ,
103+ 'Content-Type ' => 'application/json ' ,
104+ ],
105+ 'body ' => json_encode ($ body ),
106+ ]);
107+ if ($ response ->getStatusCode () == 200 ) {
108+ $ data = $ response ->getBody ()->getContents ();
74109
110+ return new JsonResponse (json_decode ($ data , true ));
111+ } else {
112+
113+ }
114+ } catch (\GuzzleHttp \Exception \ClientException $ e ) {
115+ // Capture 4xx errors (e.g., bad request)
116+ $ errorResponse = $ e ->getResponse ();
117+ $ errorData = $ errorResponse
118+ ? json_decode ($ errorResponse ->getBody ()->getContents (), true )
119+ : ['error ' => 'Unknown client error occurred. ' ];
120+ return new JsonResponse ($ errorData , $ errorResponse ->getStatusCode ());
121+ } catch (\Exception $ e ) {
122+ return new JsonResponse (['error ' => $ e ->getMessage ()], 500 );
123+ }
124+ }
75125}
0 commit comments