Skip to content

Commit 9748933

Browse files
Fix: Update API request format to match JS SDK implementation (#6)
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Max Prilutskiy <maks.prilutskiy@gmail.com>
1 parent 2334beb commit 9748933

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

src/LingoDotDevEngine.php

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -152,28 +152,30 @@ protected function localizeRaw(array $payload, array $params, ?callable $progres
152152
private function _localizeChunk(?string $sourceLocale, string $targetLocale, array $payload, string $workflowId, bool $fast): array
153153
{
154154
try {
155-
$reference = null;
155+
$requestBody = [
156+
'params' => [
157+
'workflowId' => $workflowId,
158+
'fast' => $fast
159+
],
160+
'locale' => [
161+
'source' => $sourceLocale,
162+
'target' => $targetLocale
163+
],
164+
'data' => $payload['data']
165+
];
166+
156167
if (isset($payload['reference']) && $payload['reference'] !== null) {
157168
if (!is_array($payload['reference'])) {
158169
throw new \InvalidArgumentException('Reference must be an array');
159170
}
160-
$reference = $payload['reference'];
171+
$requestBody['reference'] = $payload['reference'];
172+
} else {
173+
$requestBody['reference'] = (object)[];
161174
}
162175

163176
$response = $this->_httpClient->post(
164177
'/i18n', [
165-
'json' => [
166-
'params' => [
167-
'workflowId' => $workflowId,
168-
'fast' => $fast
169-
],
170-
'locale' => [
171-
'source' => $sourceLocale,
172-
'target' => $targetLocale
173-
],
174-
'data' => $payload['data'],
175-
'reference' => $reference
176-
]
178+
'json' => $requestBody
177179
]
178180
);
179181

@@ -384,25 +386,21 @@ public function localizeChat(array $chat, array $params, ?callable $progressCall
384386
}
385387
}
386388

387-
$payload = [];
388-
foreach ($chat as $index => $message) {
389-
$payload["chat_{$index}"] = $message['text'];
390-
}
391-
392-
$localized = $this->localizeRaw($payload, $params, function($progress, $chunk, $processedChunk) use ($progressCallback) {
389+
$localized = $this->localizeRaw(['chat' => $chat], $params, function($progress, $chunk, $processedChunk) use ($progressCallback) {
393390
if ($progressCallback) {
394391
$progressCallback($progress);
395392
}
396393
});
397394

398395
$result = [];
399-
foreach ($localized as $key => $value) {
400-
if (strpos($key, 'chat_') === 0) {
401-
$index = (int)explode('_', $key)[1];
402-
$result[] = [
403-
'name' => $chat[$index]['name'],
404-
'text' => $value
405-
];
396+
if (isset($localized['chat']) && is_array($localized['chat'])) {
397+
foreach ($localized['chat'] as $index => $message) {
398+
if (isset($chat[$index]['name']) && isset($message['text'])) {
399+
$result[] = [
400+
'name' => $chat[$index]['name'],
401+
'text' => $message['text']
402+
];
403+
}
406404
}
407405
}
408406

0 commit comments

Comments
 (0)