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. diff --git a/src/GoogleTranslate.php b/src/GoogleTranslate.php index 6a77897..615b3e7 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 ]; @@ -96,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 ]; 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) 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);