-
Notifications
You must be signed in to change notification settings - Fork 6
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 #2 from Elhebert/main
Add DeepL as translation service
- Loading branch information
Showing
9 changed files
with
252 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
.idea | ||
vendor | ||
.phpunit.result.cache | ||
.php-cs-fixer.cache |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bottelet\TranslationChecker\Translator; | ||
|
||
use Bottelet\TranslationChecker\Translator\VariableHandlers\VariableRegexHandler; | ||
use DeepL\Translator; | ||
|
||
class DeeplTranslator implements TranslatorContract | ||
{ | ||
public function __construct( | ||
protected VariableRegexHandler $variableHandler, | ||
protected Translator $translateClient, | ||
) { | ||
} | ||
|
||
/** | ||
* @throws \DeepL\DeepLException | ||
*/ | ||
public function translate(string $text, string $targetLanguage, string $sourceLanguage = 'en'): string | ||
{ | ||
$replaceVariablesText = $this->variableHandler->replacePlaceholders($text); | ||
|
||
$translation = $this->translateClient->translateText($replaceVariablesText, $sourceLanguage, $targetLanguage); | ||
|
||
return $this->variableHandler->restorePlaceholders($translation->text); | ||
} | ||
|
||
/** | ||
* @param array<string> $texts | ||
* @return array<string> | ||
* | ||
* @throws \DeepL\DeepLException | ||
*/ | ||
public function translateBatch(array $texts, string $targetLanguage, string $sourceLanguage = 'en'): array | ||
{ | ||
$textsToTranslate = array_map([$this->variableHandler, 'replacePlaceholders'], $texts); | ||
|
||
$translations = $this->translateClient->translateText($textsToTranslate, $sourceLanguage, $targetLanguage); | ||
|
||
$translatedKeys = []; | ||
foreach ($translations as $index => $translation) { | ||
$translatedKeys[$texts[$index]] = $this->variableHandler->restorePlaceholders($translation->text); | ||
} | ||
|
||
return $translatedKeys; | ||
} | ||
|
||
public function isConfigured(): bool | ||
{ | ||
/** @var array<string, null|string> $deeplConfig */ | ||
$deeplConfig = config('translator.translators.deepl'); | ||
|
||
return !in_array(null, $deeplConfig, true); | ||
} | ||
} |
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,103 @@ | ||
<?php | ||
|
||
namespace Bottelet\TranslationChecker\Tests\Translator; | ||
|
||
use Bottelet\TranslationChecker\Translator\DeeplTranslator; | ||
use Bottelet\TranslationChecker\Translator\VariableHandlers\VariableRegexHandler; | ||
use DeepL\TextResult; | ||
use DeepL\Translator; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
|
||
class DeeplTranslatorTest extends \Bottelet\TranslationChecker\Tests\TestCase | ||
{ | ||
/** @var VariableRegexHandler|MockObject */ | ||
private $variableHandlerMock; | ||
|
||
/** @var Translator|MockObject */ | ||
private $translateClientMock; | ||
|
||
/** @var DeeplTranslator */ | ||
private $deeplTranslator; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->app['config']->set('translator.translators.deepl', [ | ||
'api_key' => 'test-key', | ||
]); | ||
|
||
$this->translateClientMock = $this->createMock(Translator::class); | ||
$this->variableHandlerMock = $this->createMock(VariableRegexHandler::class); | ||
$this->deeplTranslator = new DeeplTranslator($this->variableHandlerMock, $this->translateClientMock); | ||
} | ||
|
||
#[Test] | ||
public function translate(): void | ||
{ | ||
$text = 'Hello, world!'; | ||
$translatedText = 'Bonjour le monde!'; | ||
$targetLanguage = 'fr'; | ||
|
||
$this->variableHandlerMock->method('replacePlaceholders') | ||
->willReturn($text); | ||
|
||
$this->variableHandlerMock->method('restorePlaceholders') | ||
->willReturn($translatedText); | ||
|
||
$this->translateClientMock->method('translateText') | ||
->willReturn(new TextResult($translatedText, 'en', 0)); | ||
|
||
$result = $this->deeplTranslator->translate($text, $targetLanguage); | ||
|
||
$this->assertSame($translatedText, $result); | ||
} | ||
|
||
#[Test] | ||
public function translateBatch(): void | ||
{ | ||
$texts = ['Hello, world!', 'Good morning']; | ||
$translatedTexts = ['Bonjour le monde!', 'Bonjour']; | ||
$targetLanguage = 'fr'; | ||
|
||
$translations = array_map(fn ($text) => new TextResult($text, 'en', 0), $translatedTexts); | ||
|
||
$this->variableHandlerMock->expects($this->exactly(count($texts))) | ||
->method('replacePlaceholders') | ||
->willReturnOnConsecutiveCalls(...$texts); | ||
|
||
$this->variableHandlerMock->expects($this->exactly(count($texts))) | ||
->method('restorePlaceholders') | ||
->willReturnOnConsecutiveCalls(...$translatedTexts); | ||
|
||
$this->translateClientMock->expects($this->once()) | ||
->method('translateText') | ||
->willReturn($translations); | ||
|
||
$result = $this->deeplTranslator->translateBatch($texts, $targetLanguage); | ||
|
||
$this->assertSame(['Hello, world!' => 'Bonjour le monde!', 'Good morning' => 'Bonjour'], $result); | ||
} | ||
|
||
#[Test] | ||
public function testDeeplTranslatorBinding(): void | ||
{ | ||
$this->assertInstanceOf(DeeplTranslator::class, app(DeeplTranslator::class)); | ||
} | ||
|
||
#[Test] | ||
public function testDeeplTranslatorHasValidCredentials(): void | ||
{ | ||
$this->assertTrue($this->deeplTranslator->isConfigured()); | ||
} | ||
|
||
#[Test] | ||
public function testDeeplTranslatorHasInvalidCredentials(): void | ||
{ | ||
$this->app['config']->set('translator.translators.deepl', [ | ||
'api_key' => null, | ||
]); | ||
$this->assertFalse($this->deeplTranslator->isConfigured()); | ||
} | ||
} |