-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Microsoft Translator Support * Github Actions changes
- Loading branch information
Showing
8 changed files
with
124 additions
and
5 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,5 +1,5 @@ | ||
name: Analysis | ||
on: [push, pull_request] | ||
on: [push] | ||
|
||
jobs: | ||
php-cs-fixer: | ||
|
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,5 +1,5 @@ | ||
name: Tests | ||
on: [push, pull_request] | ||
on: [push] | ||
|
||
jobs: | ||
build-test: | ||
|
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
49 changes: 49 additions & 0 deletions
49
src/DivanteTranslationBundle/Provider/MicrosoftProvider.php
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,49 @@ | ||
<?php | ||
/** | ||
* @author Piotr Rugała <[email protected]> | ||
* @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DivanteTranslationBundle\Provider; | ||
|
||
use DivanteTranslationBundle\Exception\TranslationException; | ||
|
||
class MicrosoftProvider extends AbstractProvider | ||
{ | ||
protected string $url = 'https://api.cognitive.microsofttranslator.com/'; | ||
|
||
public function translate(string $data, string $targetLanguage): string | ||
{ | ||
try { | ||
$response = $this->getHttpClient()->request( | ||
'POST', | ||
'translate', | ||
[ | ||
'headers' => [ | ||
'Ocp-Apim-Subscription-Key' => $this->apiKey, | ||
'X-ClientTraceId' => md5(uniqid((string) rand(), true)), | ||
'Content-Type' => 'application/json', | ||
], | ||
'json' => [['Text' => $data]], | ||
'query' => [ | ||
'api-version' => '3.0', | ||
'to' => locale_get_primary_language($targetLanguage), | ||
], | ||
] | ||
); | ||
$body = $response->getBody()->getContents(); | ||
$data = json_decode($body, true); | ||
} catch (\Throwable $exception) { | ||
throw new TranslationException(); | ||
} | ||
|
||
return $data[0]['translations'][0]['text']; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'microsoft_translate'; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
tests/DivanteTranslationBundle/Provider/DeeplProviderTest.php
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,4 +1,8 @@ | ||
<?php | ||
/** | ||
* @author Piotr Rugała <[email protected]> | ||
* @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
|
4 changes: 4 additions & 0 deletions
4
tests/DivanteTranslationBundle/Provider/GoogleProviderTest.php
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,4 +1,8 @@ | ||
<?php | ||
/** | ||
* @author Piotr Rugała <[email protected]> | ||
* @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
|
51 changes: 51 additions & 0 deletions
51
tests/DivanteTranslationBundle/Provider/MicrosoftProviderTest.php
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,51 @@ | ||
<?php | ||
/** | ||
* @author Piotr Rugała <[email protected]> | ||
* @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\DivanteTranslationBundle\Provider; | ||
|
||
use DivanteTranslationBundle\Provider\MicrosoftProvider; | ||
use DivanteTranslationBundle\Provider\ProviderInterface; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Psr7\Response; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class MicrosoftProviderTest extends TestCase | ||
{ | ||
public function testTranslate(): void | ||
{ | ||
$response = [ | ||
[ | ||
'translations' => [ | ||
[ | ||
'text' => 'test' | ||
], | ||
], | ||
], | ||
]; | ||
|
||
$this->assertSame('test', $this->createProvider($response)->translate('test', 'en')); | ||
} | ||
|
||
private function createProvider(array $response): ProviderInterface | ||
{ | ||
$mock = new MockHandler([ | ||
new Response(200, [], json_encode($response)), | ||
]); | ||
$handlerStack = HandlerStack::create($mock); | ||
$client = new Client(['handler' => $handlerStack]); | ||
$provider = $this->getMockBuilder(MicrosoftProvider::class) | ||
->onlyMethods(['getHttpClient']) | ||
->getMock(); | ||
$provider->method('getHttpClient')->willReturn($client); | ||
$provider->setApiKey('test'); | ||
|
||
return $provider; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/DivanteTranslationBundle/Provider/ProviderFactoryTest.php
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,4 +1,10 @@ | ||
<?php | ||
/** | ||
* @author Piotr Rugała <[email protected]> | ||
* @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\DivanteTranslationBundle\Provider; | ||
|
||
|