Skip to content

Commit

Permalink
Fixed Yandex responses
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jun 28, 2024
1 parent c5bd89e commit c682f54
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
"dragon-code/support": "^6.13",
"guzzlehttp/guzzle": "^7.8",
"illuminate/config": "^10.0 || ^11.0",
"illuminate/http": "^10.0 || ^11.0",
"illuminate/support": "^10.0 || ^11.0",
"laravel-lang/config": "^1.0@dev",
"laravel-lang/config": "^1.7",
"laravel-lang/locale-list": "^1.4",
"stichoza/google-translate-php": "^5.1"
},
Expand Down
4 changes: 3 additions & 1 deletion src/Integrations/Yandex.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function __construct(

protected function request(array|string $text, Locale|string $to, Locale|string|null $from): Collection
{
return collect($this->translator->translate($text, $this->locale($to), $this->locale($from)));
return collect($this->translator->translate($text, $this->locale($to), $this->locale($from)))->map(
fn (array $item) => $item['text']
);
}
}
11 changes: 6 additions & 5 deletions src/Requests/YandexTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace LaravelLang\Translator\Requests;

use Illuminate\Http\Client\Response;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use LaravelLang\LocaleList\Locale;
Expand All @@ -21,26 +22,26 @@ public function __construct(

public function translate(iterable|string $text, Locale|string $to, Locale|string|null $from): array
{
return $this->request($this->resolveText($text), $to, $from);
return $this->request($this->resolveText($text), $to, $from)->json('translations');
}

protected function request(array $text, ?string $to, ?string $from): array
protected function request(array $text, string $to, ?string $from): Response
{
return Http::acceptJson()
->asJson()
->withHeader('Authorization', 'Bearer ' . $this->key)
->throw()
->post($this->url, $this->body($text, $to, $from))
->throw()
->json();
->throw();
}

protected function body(array $texts, ?string $to, ?string $from): array
protected function body(array $texts, string $to, ?string $from): array
{
return collect([
'folderId' => $this->folder,
'format' => $this->format,
'texts' => $texts,
'speller' => true,

'targetLanguageCode' => $to,
])->when($from, fn (Collection $items) => $items->put('sourceLanguageCode', $from))->all();
Expand Down
10 changes: 7 additions & 3 deletions tests/Helpers/Mocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DeepL\TextResult;
use Illuminate\Support\Arr;
use LaravelLang\LocaleList\Locale;
use LaravelLang\Translator\Integrations\Deepl;
use LaravelLang\Translator\Integrations\Google;
use LaravelLang\Translator\Integrations\Yandex;
Expand Down Expand Up @@ -48,9 +49,12 @@ function mockYandexTranslator(array|string|null $text = null): void
{
$mock = mock(Yandex::$integration);

$mock->shouldReceive('translate')->andReturn(
Arr::wrap($text ?? [Value::Text1French])
);
$values = Arr::wrap($text ?? [Value::Text1French]);

$mock->shouldReceive('translate')->andReturn(array_map(fn (string $text) => [
'text' => $text,
'detectedLanguageCode' => Locale::French->value,
], $values));

mockTranslator(Yandex::class, $mock);
}
Expand Down

0 comments on commit c682f54

Please sign in to comment.