Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jun 27, 2024
1 parent ed1f0fb commit 8e32c5d
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Facades/Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @method static array|string text(iterable|string $text, Locale|string $to, Locale|string|null $from = null)
* @method static array|string viaDeepl(iterable|string $text, Locale|string $to, Locale|string|null $from = null)
* @method static array|string viaGoogleFree(iterable|string $text, Locale|string $to, Locale|string|null $from = null)
* @method static array|string viaGoogle(iterable|string $text, Locale|string $to, Locale|string|null $from = null)
* @method static array|string viaYandex(iterable|string $text, Locale|string $to, Locale|string|null $from = null)
*/
class Translate extends Facade
Expand Down
6 changes: 3 additions & 3 deletions src/Integrations/Deepl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace LaravelLang\Translator\Integrations;

use DeepL\Translator as DeeplTranslator;
use Illuminate\Support\Collection;
use LaravelLang\LocaleList\Locale;
use LaravelLang\Translator\Requests\DeeplTranslate;

class Deepl extends Integration
{
Expand Down Expand Up @@ -96,10 +96,10 @@ class Deepl extends Integration
// Locale::Welsh->value => 'cy',
];

public static string $integration = DeeplTranslator::class;
public static string $integration = DeeplTranslate::class;

public function __construct(
protected DeeplTranslator $translator
protected DeeplTranslate $translator
) {}

protected function request(iterable|string $text, Locale|string $to, Locale|string|null $from): Collection
Expand Down
6 changes: 3 additions & 3 deletions src/Integrations/Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Illuminate\Support\Collection;
use LaravelLang\LocaleList\Locale;
use Stichoza\GoogleTranslate\GoogleTranslate;
use LaravelLang\Translator\Requests\GoogleTranslate;

class Google extends Integration
{
Expand Down Expand Up @@ -142,7 +142,7 @@ class Google extends Integration

public function __construct(
protected GoogleTranslate $translator,
protected bool|string $regex = true
protected bool|string $preserveParameters = true
) {}

protected function request(iterable|string $text, Locale|string $to, Locale|string|null $from): Collection
Expand All @@ -155,7 +155,7 @@ protected function request(iterable|string $text, Locale|string $to, Locale|stri
protected function translator(Locale|string $to, Locale|string|null $from): GoogleTranslate
{
return $this->translator
->preserveParameters($this->regex)
->preserveParameters($this->preserveParameters)
->setSource($this->locale($from))
->setTarget($this->locale($to));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Integrations/Yandex.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

use Illuminate\Support\Collection;
use LaravelLang\LocaleList\Locale;
use LaravelLang\Translator\Requests\YandexCloud;
use LaravelLang\Translator\Requests\YandexTranslate;

class Yandex extends Integration
{
protected array $map = [
Locale::French->value => 'fr',
];

public static string $integration = YandexCloud::class;
public static string $integration = YandexTranslate::class;

public function __construct(
protected YandexCloud $translator,
protected YandexTranslate $translator,
) {}

protected function request(iterable|string $text, Locale|string $to, Locale|string|null $from): Collection
Expand Down
16 changes: 16 additions & 0 deletions src/Requests/DeeplTranslate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Translator\Requests;

use DeepL\Translator as DeeplTranslator;

class DeeplTranslate extends DeeplTranslator
{
public function __construct(
string $key
) {
parent::__construct($key);
}
}
46 changes: 46 additions & 0 deletions src/Requests/GoogleTranslate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Translator\Requests;

use Stichoza\GoogleTranslate\GoogleTranslate as BaseTranslate;

class GoogleTranslate extends BaseTranslate
{
/**
* Extract replaceable keywords from string using the supplied pattern.
*/
protected function extractParameters(string $string): string
{
if (! $this->pattern) {
return $string;
}

return preg_replace_callback(
$this->pattern,
function ($matches) {
static $index = -1;

++$index;

return '{' . $index . '}';
},
$string
);
}

/**
* Inject the replacements back into the translated string.
*
* @param array<string> $replacements
*/
protected function injectParameters(string $string, array $replacements): string
{
return preg_replace_callback(
'/\{(\d+)}/',
fn ($matches) => $replacements[$matches[1]],
$string
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
use Illuminate\Support\Facades\Http;
use LaravelLang\LocaleList\Locale;

class YandexCloud
class YandexTranslate
{
protected string $url = 'https://translate.api.cloud.yandex.net/translate/v2/translate';

protected string $format = 'PLAIN_TEXT';

public function __construct(
protected string $key,
protected string $folderId,
protected string $folder,
) {}

public function translate(iterable|string $text, Locale|string $to, Locale|string|null $from): array
Expand All @@ -38,7 +38,7 @@ protected function request(array $text, ?string $to, ?string $from): array
protected function body(array $texts, ?string $to, ?string $from): array
{
return collect([
'folderId' => $this->folderId,
'folderId' => $this->folder,
'format' => $this->format,
'texts' => $texts,

Expand Down
4 changes: 2 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace LaravelLang\Translator;

use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use LaravelLang\Config\Data\Shared\TranslatorData;
use LaravelLang\Config\Data\Shared\Translators\TranslatorData;
use LaravelLang\Config\Facades\Config;
use LaravelLang\Translator\Contracts\Translator;

Expand All @@ -30,6 +30,6 @@ protected function bootTranslator(string|Translator $translator, array $credenti
*/
protected function translators(): array
{
return Config::shared()->translators->enabled;
return Config::shared()->translators->channels->all;
}
}
13 changes: 5 additions & 8 deletions src/Services/Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace LaravelLang\Translator\Services;

use LaravelLang\Config\Data\Shared\TranslatorData;
use LaravelLang\Config\Data\Shared\Translators\TranslatorData;
use LaravelLang\Config\Facades\Config;
use LaravelLang\LocaleList\Locale;
use LaravelLang\Translator\Contracts\Translator;
Expand All @@ -30,11 +30,8 @@ public function viaDeepl(iterable|string $text, Locale|string $to, Locale|string
return $this->via(Deepl::class, $text, $to, $from);
}

public function viaGoogleFree(
iterable|string $text,
Locale|string $to,
Locale|string|null $from = null
): array|string {
public function viaGoogle(iterable|string $text, Locale|string $to, Locale|string|null $from = null): array|string
{
return $this->via(Google::class, $text, $to, $from);
}

Expand Down Expand Up @@ -72,11 +69,11 @@ protected function translate(
*/
protected function translators(): array
{
return Config::shared()->translators->enabled;
return Config::shared()->translators->channels->enabled;
}

protected function initialize(string $class): Translator
{
return app($class);
return app(ltrim($class, '\\'));
}
}
9 changes: 3 additions & 6 deletions tests/Helpers/Mocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

declare(strict_types=1);

use DeepL\Translator as DeeplTranslate;
use Illuminate\Support\Arr;
use LaravelLang\Translator\Integrations\Deepl;
use LaravelLang\Translator\Integrations\Google;
use LaravelLang\Translator\Integrations\Yandex;
use LaravelLang\Translator\Requests\YandexCloud as YandexTranslate;
use Stichoza\GoogleTranslate\GoogleTranslate;
use Tests\Constants\Value;

function mockTranslators(array|string|null $text = null): void
Expand All @@ -20,7 +17,7 @@ function mockTranslators(array|string|null $text = null): void

function mockDeeplTranslator(array|string|null $text = null): void
{
$mock = mock(DeeplTranslate::class);
$mock = mock(Deepl::$integration);

$mock->shouldReceive('translateText')->andReturn($text ?? Value::Text1French);

Expand All @@ -29,7 +26,7 @@ function mockDeeplTranslator(array|string|null $text = null): void

function mockGoogleTranslator(array|string|null $text = null): void
{
$mock = mock(GoogleTranslate::class);
$mock = mock(Google::$integration);

$mock->shouldReceive('preserveParameters', 'setSource', 'setTarget')->andReturnSelf();

Expand All @@ -42,7 +39,7 @@ function mockGoogleTranslator(array|string|null $text = null): void

function mockYandexTranslator(array|string|null $text = null): void
{
$mock = mock(YandexTranslate::class);
$mock = mock(Yandex::$integration);

$mock->shouldReceive('translate')->andReturn(
Arr::wrap($text ?? [Value::Text1French])
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Translate/FacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
);
});

test('via google free', function () {
expect(Translate::viaGoogleFree(Value::Text1English, Locale::French))->toBe(
test('via google', function () {
expect(Translate::viaGoogle(Value::Text1English, Locale::French))->toBe(
Value::Text1French
);
});
Expand Down

0 comments on commit 8e32c5d

Please sign in to comment.