Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from ben182/develop
0.5.0
- Loading branch information
Showing
10 changed files
with
146 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Ben182\AutoTranslate\Translators; | ||
|
||
use Scn\DeeplApiConnector\DeeplClient; | ||
use Scn\DeeplApiConnector\Model\TranslationConfig; | ||
use Scn\DeeplApiConnector\Exception\RequestException; | ||
use Ben182\AutoTranslate\Exceptions\LanguageCodeNotExist; | ||
|
||
class DeeplTranslator implements TranslatorInterface | ||
{ | ||
protected $translator; | ||
protected $source; | ||
protected $target; | ||
|
||
public function __construct() | ||
{ | ||
$this->translator = DeeplClient::create(config('auto-translate.deepl.api_key')); | ||
} | ||
|
||
public function setSource(string $source) | ||
{ | ||
$this->source = strtoupper($source); | ||
|
||
return $this; | ||
} | ||
|
||
public function setTarget(string $target) | ||
{ | ||
$this->target = strtoupper($target); | ||
|
||
return $this; | ||
} | ||
|
||
public function translate(string $string) : string | ||
{ | ||
$translation = new TranslationConfig( | ||
$string, | ||
$this->target, | ||
$this->source | ||
); | ||
|
||
try { | ||
return $this->translator->getTranslation($translation)->getText(); | ||
} catch (RequestException $th) { | ||
if ($th->getMessage() === '400 {"message":"Value for \'target_lang\' is not supported."}') { | ||
throw LanguageCodeNotExist::throw($this->source, $this->target); | ||
} | ||
|
||
throw $th; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters