Skip to content

Commit

Permalink
Merge pull request #34 from resslinger/master
Browse files Browse the repository at this point in the history
add param for source language
  • Loading branch information
introwit authored Mar 7, 2022
2 parents 4e8b3f0 + 0d6fc03 commit f18d479
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
7 changes: 7 additions & 0 deletions config/googletranslate.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?php

return [
/*
|----------------------------------------------------------------------------------------------------
| The ISO 639-1 code of the default source language.
|----------------------------------------------------------------------------------------------------
*/
'default_source_translation' => 'en',

/*
|----------------------------------------------------------------------------------------------------
| The ISO 639-1 code of the language in lowercase to which the text will be translated to by default.
Expand Down
17 changes: 10 additions & 7 deletions src/GoogleTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
Expand All @@ -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
];
Expand Down
8 changes: 4 additions & 4 deletions src/GoogleTranslateClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions tests/GoogleTranslateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down

0 comments on commit f18d479

Please sign in to comment.