@@ -94,12 +94,20 @@ public function __construct(array $config = [])
9494 * @return array Localized content
9595 * @internal
9696 */
97- protected function localizeRaw (array $ payload , array $ params , callable $ progressCallback = null ): array
97+ protected function localizeRaw (array $ payload , array $ params , ? callable $ progressCallback = null ): array
9898 {
9999 if (!isset ($ params ['targetLocale ' ])) {
100100 throw new \InvalidArgumentException ('Target locale is required ' );
101101 }
102102
103+ if (isset ($ params ['sourceLocale ' ]) && !is_string ($ params ['sourceLocale ' ]) && $ params ['sourceLocale ' ] !== null ) {
104+ throw new \InvalidArgumentException ('Source locale must be a string or null ' );
105+ }
106+
107+ if (!is_string ($ params ['targetLocale ' ])) {
108+ throw new \InvalidArgumentException ('Target locale must be a string ' );
109+ }
110+
103111 $ chunkedPayload = $ this ->_extractPayloadChunks ($ payload );
104112 $ processedPayloadChunks = [];
105113
@@ -112,7 +120,10 @@ protected function localizeRaw(array $payload, array $params, callable $progress
112120 $ processedPayloadChunk = $ this ->_localizeChunk (
113121 $ params ['sourceLocale ' ] ?? null ,
114122 $ params ['targetLocale ' ],
115- ['data ' => $ chunk , 'reference ' => $ params ['reference ' ] ?? null ],
123+ [
124+ 'data ' => $ chunk ,
125+ 'reference ' => $ params ['reference ' ] ?? null
126+ ],
116127 $ workflowId ,
117128 $ params ['fast ' ] ?? false
118129 );
@@ -141,6 +152,14 @@ protected function localizeRaw(array $payload, array $params, callable $progress
141152 private function _localizeChunk (?string $ sourceLocale , string $ targetLocale , array $ payload , string $ workflowId , bool $ fast ): array
142153 {
143154 try {
155+ $ reference = null ;
156+ if (isset ($ payload ['reference ' ]) && $ payload ['reference ' ] !== null ) {
157+ if (!is_array ($ payload ['reference ' ])) {
158+ throw new \InvalidArgumentException ('Reference must be an array ' );
159+ }
160+ $ reference = $ payload ['reference ' ];
161+ }
162+
144163 $ response = $ this ->_httpClient ->post (
145164 '/i18n ' , [
146165 'json ' => [
@@ -153,7 +172,7 @@ private function _localizeChunk(?string $sourceLocale, string $targetLocale, arr
153172 'target ' => $ targetLocale
154173 ],
155174 'data ' => $ payload ['data ' ],
156- 'reference ' => $ payload [ ' reference ' ]
175+ 'reference ' => $ reference
157176 ]
158177 ]
159178 );
@@ -166,8 +185,17 @@ private function _localizeChunk(?string $sourceLocale, string $targetLocale, arr
166185
167186 return $ jsonResponse ['data ' ] ?? [];
168187 } catch (RequestException $ e ) {
169- if ($ e ->getResponse () && $ e ->getResponse ()->getStatusCode () === 400 ) {
170- throw new \InvalidArgumentException ('Invalid request: ' . $ e ->getMessage ());
188+ if ($ e ->hasResponse ()) {
189+ $ statusCode = $ e ->getResponse ()->getStatusCode ();
190+ $ responseBody = $ e ->getResponse ()->getBody ()->getContents ();
191+
192+ if ($ statusCode === 400 ) {
193+ throw new \InvalidArgumentException ('Invalid request: ' . $ e ->getMessage ());
194+ } else {
195+ $ errorData = json_decode ($ responseBody , true );
196+ $ errorMessage = isset ($ errorData ['message ' ]) ? $ errorData ['message ' ] : $ e ->getMessage ();
197+ throw new \RuntimeException ($ errorMessage );
198+ }
171199 }
172200 throw new \RuntimeException ($ e ->getMessage ());
173201 }
@@ -261,9 +289,17 @@ private function _createId(): string
261289 *
262290 * @return array A new object with the same structure but localized string values
263291 */
264- public function localizeObject (array $ obj , array $ params , callable $ progressCallback = null ): array
292+ public function localizeObject (array $ obj , array $ params , ? callable $ progressCallback = null ): array
265293 {
266- return $ this ->localizeRaw ($ obj , $ params , $ progressCallback );
294+ if (!isset ($ params ['targetLocale ' ])) {
295+ throw new \InvalidArgumentException ('Target locale is required ' );
296+ }
297+
298+ return $ this ->localizeRaw ($ obj , $ params , function ($ progress , $ chunk , $ processedChunk ) use ($ progressCallback ) {
299+ if ($ progressCallback ) {
300+ $ progressCallback ($ progress , $ chunk , $ processedChunk );
301+ }
302+ });
267303 }
268304
269305 /**
@@ -278,9 +314,18 @@ public function localizeObject(array $obj, array $params, callable $progressCall
278314 *
279315 * @return string The localized text string
280316 */
281- public function localizeText (string $ text , array $ params , callable $ progressCallback = null ): string
317+ public function localizeText (string $ text , array $ params , ? callable $ progressCallback = null ): string
282318 {
283- $ response = $ this ->localizeRaw (['text ' => $ text ], $ params , $ progressCallback );
319+ if (!isset ($ params ['targetLocale ' ])) {
320+ throw new \InvalidArgumentException ('Target locale is required ' );
321+ }
322+
323+ $ response = $ this ->localizeRaw (['text ' => $ text ], $ params , function ($ progress , $ chunk , $ processedChunk ) use ($ progressCallback ) {
324+ if ($ progressCallback ) {
325+ $ progressCallback ($ progress );
326+ }
327+ });
328+
284329 return $ response ['text ' ] ?? '' ;
285330 }
286331
@@ -331,25 +376,34 @@ public function batchLocalizeText(string $text, array $params): array
331376 *
332377 * @return array Array of localized chat messages with preserved structure
333378 */
334- public function localizeChat (array $ chat , array $ params , callable $ progressCallback = null ): array
379+ public function localizeChat (array $ chat , array $ params , ? callable $ progressCallback = null ): array
335380 {
336- $ payload = [];
337- foreach ($ chat as $ index => $ message ) {
381+ foreach ($ chat as $ message ) {
338382 if (!isset ($ message ['name ' ]) || !isset ($ message ['text ' ])) {
339383 throw new \InvalidArgumentException ('Each chat message must have name and text properties ' );
340384 }
385+ }
386+
387+ $ payload = [];
388+ foreach ($ chat as $ index => $ message ) {
341389 $ payload ["chat_ {$ index }" ] = $ message ['text ' ];
342390 }
343391
344- $ localized = $ this ->localizeRaw ($ payload , $ params , $ progressCallback );
392+ $ localized = $ this ->localizeRaw ($ payload , $ params , function ($ progress , $ chunk , $ processedChunk ) use ($ progressCallback ) {
393+ if ($ progressCallback ) {
394+ $ progressCallback ($ progress );
395+ }
396+ });
345397
346398 $ result = [];
347399 foreach ($ localized as $ key => $ value ) {
348- $ index = (int )explode ('_ ' , $ key )[1 ];
349- $ result [] = [
350- 'name ' => $ chat [$ index ]['name ' ],
351- 'text ' => $ value
352- ];
400+ if (strpos ($ key , 'chat_ ' ) === 0 ) {
401+ $ index = (int )explode ('_ ' , $ key )[1 ];
402+ $ result [] = [
403+ 'name ' => $ chat [$ index ]['name ' ],
404+ 'text ' => $ value
405+ ];
406+ }
353407 }
354408
355409 return $ result ;
@@ -364,6 +418,10 @@ public function localizeChat(array $chat, array $params, callable $progressCallb
364418 */
365419 public function recognizeLocale (string $ text ): string
366420 {
421+ if (empty (trim ($ text ))) {
422+ throw new \InvalidArgumentException ('Text cannot be empty ' );
423+ }
424+
367425 try {
368426 $ response = $ this ->_httpClient ->post (
369427 '/recognize ' , [
@@ -372,8 +430,20 @@ public function recognizeLocale(string $text): string
372430 );
373431
374432 $ jsonResponse = json_decode ($ response ->getBody ()->getContents (), true );
433+
434+ if (!isset ($ jsonResponse ['locale ' ])) {
435+ throw new \RuntimeException ('Invalid response from API: locale not found ' );
436+ }
437+
375438 return $ jsonResponse ['locale ' ];
376439 } catch (RequestException $ e ) {
440+ if ($ e ->hasResponse ()) {
441+ $ statusCode = $ e ->getResponse ()->getStatusCode ();
442+ $ responseBody = $ e ->getResponse ()->getBody ()->getContents ();
443+ $ errorData = json_decode ($ responseBody , true );
444+ $ errorMessage = isset ($ errorData ['message ' ]) ? $ errorData ['message ' ] : $ e ->getMessage ();
445+ throw new \RuntimeException ('Error recognizing locale: ' . $ errorMessage );
446+ }
377447 throw new \RuntimeException ('Error recognizing locale: ' . $ e ->getMessage ());
378448 }
379449 }
0 commit comments