Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add param for source language #34

Merged
merged 7 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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