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

Allow empty string as source language for auto-detection #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions config/googletranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
/*
|----------------------------------------------------------------------------------------------------
| The ISO 639-1 code of the default source language.
|
| You may as well leave an empty string '' for automatic source language detection.
|----------------------------------------------------------------------------------------------------
*/
'default_source_translation' => 'en',
Expand Down
23 changes: 14 additions & 9 deletions src/GoogleTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,23 @@ public function translate($input, $from = null, $to = null, $format = 'text'): a
{
$this->validateInput($input);

$translateFrom = $from ?? config('googletranslate.default_source_translation');
$translateFrom = $from === null ? config('googletranslate.default_source_translation') : $from;
$translateTo = $to ?? config('googletranslate.default_target_translation');

$translateFrom = $this->sanitizeLanguageCode($translateFrom);
$translateTo = $this->sanitizeLanguageCode($translateTo);

if (is_array($input)) {
return $this->translateBatch($input, $translateFrom, $translateTo, $format);
}

$translateFrom = $this->sanitizeLanguageCode($translateFrom, true);
$translateTo = $this->sanitizeLanguageCode($translateTo);

$response = $this
->translateClient
->translate($input, $translateFrom, $translateTo, $format);

return [
'source_text' => $input,
'source_language_code' => $translateFrom,
'source_language_code' => $response['source'],
'translated_text' => $response['text'],
'translated_language_code' => $translateTo
];
Expand All @@ -90,7 +90,7 @@ public function justTranslate(string $input, $from = null, $to = null): string
$translateFrom = $from ?? config('googletranslate.default_source_translation');
$translateTo = $to ?? config('googletranslate.default_target_translation');

$translateFrom = $this->sanitizeLanguageCode($translateFrom);
$translateFrom = $this->sanitizeLanguageCode($translateFrom, true);
$translateTo = $this->sanitizeLanguageCode($translateTo);

$response = $this
Expand All @@ -102,7 +102,7 @@ public function justTranslate(string $input, $from = null, $to = null): string

public function translateBatch(array $input, string $translateFrom, string $translateTo, $format = 'text'): array
{
$translateFrom = $this->sanitizeLanguageCode($translateFrom);
$translateFrom = $this->sanitizeLanguageCode($translateFrom, true);
$translateTo = $this->sanitizeLanguageCode($translateTo);

$this->validateInput($input);
Expand All @@ -114,7 +114,7 @@ public function translateBatch(array $input, string $translateFrom, string $tran
foreach ($responses as $response) {
$translations[] = [
'source_text' => $response['input'],
'source_language_code' => $translateFrom,
'source_language_code' => $response['source'],
'translated_text' => $response['text'],
'translated_language_code' => $translateTo
];
Expand Down Expand Up @@ -150,8 +150,13 @@ public function unlessLanguageIs(string $languageCode, string $input, $from = nu
return $input;
}

public function sanitizeLanguageCode(string $languageCode)
public function sanitizeLanguageCode(string $languageCode, $allowEmptyString = false)
{
// we can pass '' as source language for auto-detection
if ($allowEmptyString && $languageCode === '') {
return $languageCode;
}

$languageCode = trim(strtolower($languageCode));

if ($languageCode === 'zh-tw') {
Expand Down