diff --git a/src/Facades/Translate.php b/src/Facades/Translate.php index e09e3a1..85c07ee 100644 --- a/src/Facades/Translate.php +++ b/src/Facades/Translate.php @@ -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 diff --git a/src/Integrations/Deepl.php b/src/Integrations/Deepl.php index 45faf25..de0dd1d 100644 --- a/src/Integrations/Deepl.php +++ b/src/Integrations/Deepl.php @@ -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 { @@ -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 diff --git a/src/Integrations/Google.php b/src/Integrations/Google.php index c96f97e..4cc37d5 100644 --- a/src/Integrations/Google.php +++ b/src/Integrations/Google.php @@ -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 { @@ -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 @@ -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)); } diff --git a/src/Integrations/Yandex.php b/src/Integrations/Yandex.php index 6af61ce..794fa3c 100644 --- a/src/Integrations/Yandex.php +++ b/src/Integrations/Yandex.php @@ -6,7 +6,7 @@ use Illuminate\Support\Collection; use LaravelLang\LocaleList\Locale; -use LaravelLang\Translator\Requests\YandexCloud; +use LaravelLang\Translator\Requests\YandexTranslate; class Yandex extends Integration { @@ -14,10 +14,10 @@ class Yandex extends Integration 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 diff --git a/src/Requests/DeeplTranslate.php b/src/Requests/DeeplTranslate.php new file mode 100644 index 0000000..73654b0 --- /dev/null +++ b/src/Requests/DeeplTranslate.php @@ -0,0 +1,16 @@ +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 $replacements + */ + protected function injectParameters(string $string, array $replacements): string + { + return preg_replace_callback( + '/\{(\d+)}/', + fn ($matches) => $replacements[$matches[1]], + $string + ); + } +} diff --git a/src/Requests/YandexCloud.php b/src/Requests/YandexTranslate.php similarity index 93% rename from src/Requests/YandexCloud.php rename to src/Requests/YandexTranslate.php index e8bcb9d..8126ce0 100644 --- a/src/Requests/YandexCloud.php +++ b/src/Requests/YandexTranslate.php @@ -8,7 +8,7 @@ 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'; @@ -16,7 +16,7 @@ class YandexCloud 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 @@ -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, diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 04ba707..1f82015 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -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; @@ -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; } } diff --git a/src/Services/Translate.php b/src/Services/Translate.php index 2ac18cd..5ae6a4d 100644 --- a/src/Services/Translate.php +++ b/src/Services/Translate.php @@ -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; @@ -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); } @@ -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, '\\')); } } diff --git a/tests/Helpers/Mocks.php b/tests/Helpers/Mocks.php index f58d430..08918a0 100644 --- a/tests/Helpers/Mocks.php +++ b/tests/Helpers/Mocks.php @@ -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 @@ -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); @@ -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(); @@ -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]) diff --git a/tests/Unit/Translate/FacadeTest.php b/tests/Unit/Translate/FacadeTest.php index 1dc8adc..aeb760d 100644 --- a/tests/Unit/Translate/FacadeTest.php +++ b/tests/Unit/Translate/FacadeTest.php @@ -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 ); });