From 3632d50e72af4f265fa435246721a437405dcfff Mon Sep 17 00:00:00 2001 From: Florian Ressel Date: Wed, 19 Jan 2022 15:32:32 +0100 Subject: [PATCH 1/7] add source language --- src/GoogleTranslate.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/GoogleTranslate.php b/src/GoogleTranslate.php index 6a77897..254b1e3 100644 --- a/src/GoogleTranslate.php +++ b/src/GoogleTranslate.php @@ -57,25 +57,27 @@ public function detectLanguageBatch(array $input): array return $translations; } - public function translate($input, $to = null, $format = 'text'): array + public function translate($input, $from = null, $to = null, $format = 'text'): array { $this->validateInput($input); + $translateFrom = $from ?? config('googletranslate.default_source_translation'); $translateTo = $to ?? config('googletranslate.default_target_translation'); + $translateFrom = $this->sanitizeLanguageCode($translateFrom); $translateTo = $this->sanitizeLanguageCode($translateTo); if (is_array($input)) { - return $this->translateBatch($input, $translateTo, $format); + return $this->translateBatch($input, $translateFrom, $translateTo, $format); } $response = $this ->translateClient - ->translate($input, $translateTo, $format); + ->translate($input, $translateFrom, $translateTo, $format); return [ 'source_text' => $input, - 'source_language_code' => $response['source'], + 'source_language_code' => $translateFrom, 'translated_text' => $response['text'], 'translated_language_code' => $translateTo ]; From f036916b0f4b06a9b6c16b92a03f8fb1f79ea3f5 Mon Sep 17 00:00:00 2001 From: Florian Ressel Date: Wed, 19 Jan 2022 15:34:40 +0100 Subject: [PATCH 2/7] add $translateFrom --- src/GoogleTranslate.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/GoogleTranslate.php b/src/GoogleTranslate.php index 254b1e3..615b3e7 100644 --- a/src/GoogleTranslate.php +++ b/src/GoogleTranslate.php @@ -98,20 +98,21 @@ public function justTranslate(string $input, $to = null): string return $response['text']; } - public function translateBatch(array $input, string $translateTo, $format = 'text'): array + public function translateBatch(array $input, string $translateFrom, string $translateTo, $format = 'text'): array { + $translateFrom = $this->sanitizeLanguageCode($translateFrom); $translateTo = $this->sanitizeLanguageCode($translateTo); $this->validateInput($input); $responses = $this ->translateClient - ->translateBatch($input, $translateTo, $format); + ->translateBatch($input, $translateFrom, $translateTo, $format); foreach ($responses as $response) { $translations[] = [ 'source_text' => $response['input'], - 'source_language_code' => $response['source'], + 'source_language_code' => $translateFrom, 'translated_text' => $response['text'], 'translated_language_code' => $translateTo ]; From 5803375b1e229867dbd128a55fe8b1e5b9006691 Mon Sep 17 00:00:00 2001 From: Florian Ressel Date: Wed, 19 Jan 2022 15:36:12 +0100 Subject: [PATCH 3/7] add translateFrom to client --- src/GoogleTranslateClient.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/GoogleTranslateClient.php b/src/GoogleTranslateClient.php index a18d425..208f906 100644 --- a/src/GoogleTranslateClient.php +++ b/src/GoogleTranslateClient.php @@ -32,16 +32,16 @@ public function detectLanguageBatch(array $input) ->detectLanguageBatch($input); } - public function translate(string $text, string $translateTo, string $format = 'text') + public function translate(string $text, string $translateFrom, string $translateTo, string $format = 'text') { return $this->translate - ->translate($text, ['target' => $translateTo, 'format' => $format]); + ->translate($text, ['source' => $translateFrom, 'target' => $translateTo, 'format' => $format]); } - public function translateBatch(array $input, string $translateTo, string $format = 'text') + public function translateBatch(array $input, string $translateFrom, string $translateTo, string $format = 'text') { return $this->translate - ->translateBatch($input, ['target' => $translateTo, 'format' => $format]); + ->translateBatch($input, ['source' => $translateFrom, 'target' => $translateTo, 'format' => $format]); } public function getAvaliableTranslationsFor(string $languageCode) From 28b37c6539499fc99e9e24e17382516702db3a41 Mon Sep 17 00:00:00 2001 From: Florian Ressel Date: Wed, 19 Jan 2022 15:38:42 +0100 Subject: [PATCH 4/7] add config key default_source_language --- config/googletranslate.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/config/googletranslate.php b/config/googletranslate.php index 0639db1..ad3b99f 100644 --- a/config/googletranslate.php +++ b/config/googletranslate.php @@ -1,6 +1,13 @@ 'en', + /* |---------------------------------------------------------------------------------------------------- | The ISO 639-1 code of the language in lowercase to which the text will be translated to by default. From 932b32ba5ccb8601f696e93b9299dfa988cd929e Mon Sep 17 00:00:00 2001 From: Florian Ressel Date: Wed, 19 Jan 2022 15:47:17 +0100 Subject: [PATCH 5/7] change test for changes --- tests/GoogleTranslateTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/GoogleTranslateTest.php b/tests/GoogleTranslateTest.php index c3f0648..4e7f59d 100644 --- a/tests/GoogleTranslateTest.php +++ b/tests/GoogleTranslateTest.php @@ -74,11 +74,11 @@ public function it_can_detect_the_language_of_an_array_of_strings_passed_to_it() public function it_can_translate_the_string_passed_to_it() { $this->translateClient - ->shouldReceive('translate')->with($this->testString, 'hi', 'text') + ->shouldReceive('translate')->with($this->testString, 'en', 'hi', 'text') ->once() ->andReturn(['source' => 'en', 'text' => '']); - $response = $this->translate->translate($this->testString, 'hi'); + $response = $this->translate->translate($this->testString, 'en', 'hi'); $this->assertIsArray($response); @@ -92,11 +92,11 @@ public function it_can_translate_the_string_passed_to_it() public function it_can_translate_the_html_string_passed_to_it() { $this->translateClient - ->shouldReceive('translate')->with($this->testHtmlString, 'hi', 'html') + ->shouldReceive('translate')->with($this->testHtmlString, 'en', 'hi', 'html') ->once() ->andReturn(['source' => 'en', 'text' => '']); - $response = $this->translate->translate($this->testHtmlString, 'hi', 'html'); + $response = $this->translate->translate($this->testHtmlString, 'en', 'hi', 'html'); $this->assertIsArray($response); @@ -110,14 +110,14 @@ public function it_can_translate_the_html_string_passed_to_it() public function it_can_translate_an_array_of_strings_passed_to_it() { $this->translateClient - ->shouldReceive('translateBatch')->with([$this->testString, $this->testString], 'hi', 'text') + ->shouldReceive('translateBatch')->with([$this->testString, $this->testString], 'en', 'hi', 'text') ->once() ->andReturn([ ['source' => 'en', 'text' => '', 'input' => $this->testString], ['source' => 'en', 'text' => '', 'input' => $this->testString] ]); - $response = $this->translate->translate([$this->testString, $this->testString], 'hi'); + $response = $this->translate->translate([$this->testString, $this->testString], 'en', 'hi'); $this->assertIsArray($response); From cccd50a3c0206b887c452777811478ab85c07fff Mon Sep 17 00:00:00 2001 From: Florian Ressel Date: Wed, 9 Feb 2022 08:06:22 +0100 Subject: [PATCH 6/7] add support for Laravel 9 --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index e10b073..b52d479 100644 --- a/composer.json +++ b/composer.json @@ -29,11 +29,11 @@ "require": { "php": "^7.4|^8.0", "google/cloud-translate": "^1.2", - "illuminate/support": "7.*|8.*" + "illuminate/support": "7.*|8.*|9.*" }, "require-dev": { "mockery/mockery": "^1.1", - "orchestra/testbench" : "5.*|6.*", + "orchestra/testbench" : "5.*|6.*|7.*", "phpunit/phpunit": "^9.0" }, "extra": { From 0d6fc0343163ce621e1f3c9f774aeab598b93eac Mon Sep 17 00:00:00 2001 From: Florian Ressel Date: Fri, 4 Mar 2022 14:07:37 +0100 Subject: [PATCH 7/7] revert changes --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index b52d479..e10b073 100644 --- a/composer.json +++ b/composer.json @@ -29,11 +29,11 @@ "require": { "php": "^7.4|^8.0", "google/cloud-translate": "^1.2", - "illuminate/support": "7.*|8.*|9.*" + "illuminate/support": "7.*|8.*" }, "require-dev": { "mockery/mockery": "^1.1", - "orchestra/testbench" : "5.*|6.*|7.*", + "orchestra/testbench" : "5.*|6.*", "phpunit/phpunit": "^9.0" }, "extra": {