-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support mutiple strings in same request
- Loading branch information
Showing
12 changed files
with
291 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the PHP Translation package. | ||
* | ||
* (c) PHP Translation team <tobias.nyholm@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
*/ | ||
|
||
namespace Translation\Translator\Service; | ||
|
||
use Http\Client\HttpClient; | ||
use Http\Message\RequestFactory; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Translation\Translator\Exception\ResponseException; | ||
|
||
/** | ||
* @author Baptiste Leduc <baptiste.leduc@gmail.com> | ||
*/ | ||
class BingTranslator extends HttpTranslator | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $key; | ||
|
||
/** | ||
* @param string $key Google API key | ||
* @param HttpClient|null $httpClient | ||
* @param RequestFactory|null $requestFactory | ||
*/ | ||
public function __construct($key, HttpClient $httpClient = null, RequestFactory $requestFactory = null) | ||
{ | ||
parent::__construct($httpClient, $requestFactory); | ||
if (empty($key)) { | ||
throw new \InvalidArgumentException('Bing "key" can not be empty'); | ||
} | ||
|
||
$this->key = $key; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function translate($string, $from, $to) | ||
{ | ||
$body = json_encode([['Text' => $string]]); | ||
$url = $this->getUrl($from, $to); | ||
$request = $this->getRequestFactory()->createRequest('POST', $url, [], $body); | ||
|
||
$request = $request | ||
->withHeader('Ocp-Apim-Subscription-Key', $this->key) | ||
->withHeader('Content-Type', 'application/json') | ||
->withHeader('X-ClientTraceId', $this->createGuid()) | ||
->withHeader('Content-length', strlen($body)); | ||
|
||
/** @var ResponseInterface $response */ | ||
$response = $this->getHttpClient()->sendRequest($request); | ||
|
||
if (200 !== $response->getStatusCode()) { | ||
throw ResponseException::createNonSuccessfulResponse($this->getUrl($string, $from, $to, '[key]')); | ||
} | ||
|
||
$responseBody = $response->getBody()->__toString(); | ||
$data = json_decode($responseBody, true); | ||
|
||
if (!is_array($data)) { | ||
throw ResponseException::createUnexpectedResponse($url, $responseBody); | ||
} | ||
|
||
foreach ($data as $details) { | ||
return $this->format($string, $details['translations'][0]['text']); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $from | ||
* @param string $to | ||
* | ||
* @return string | ||
*/ | ||
private function getUrl($from, $to) | ||
{ | ||
return sprintf( | ||
'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=%s&from=%s&textType=html', | ||
$to, | ||
$from | ||
); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
private function createGuid() | ||
{ | ||
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', | ||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), | ||
mt_rand(0, 0xffff), | ||
mt_rand(0, 0x0fff) | 0x4000, | ||
mt_rand(0, 0x3fff) | 0x8000, | ||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) | ||
); | ||
} | ||
} |
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,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the PHP Translation package. | ||
* | ||
* (c) PHP Translation team <tobias.nyholm@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
*/ | ||
|
||
namespace Translation\translator\tests\Integration; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Translation\Translator\TranslatorService; | ||
|
||
/** | ||
* @author Baptiste Leduc <baptiste.leduc@gmail.com> | ||
*/ | ||
abstract class AbstractTranslatorTest extends TestCase | ||
{ | ||
/** | ||
* @var TranslatorService | ||
*/ | ||
protected $translator = null; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $requested = ['apple', 'cherry']; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $expected = ['pomme', 'cerise']; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $from = 'en'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $to = 'fr'; | ||
|
||
public function testTranslate() | ||
{ | ||
if (null === $this->translator) { | ||
$this->markTestSkipped('No translator set.'); | ||
} | ||
|
||
$result = $this->translator->translate($this->requested[0], $this->from, $this->to); | ||
$this->assertEquals($this->expected[0], $result); | ||
|
||
$results = $this->translator->translateArray($this->requested, $this->from, $this->to); | ||
$this->assertEquals($this->expected, $results); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the PHP Translation package. | ||
* | ||
* (c) PHP Translation team <tobias.nyholm@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
*/ | ||
|
||
namespace Translation\translator\tests\Integration; | ||
|
||
use Translation\Translator\Service\BingTranslator; | ||
|
||
/** | ||
* @author Baptiste Leduc <baptiste.leduc@gmail.com> | ||
*/ | ||
class BingTranslatorTest extends AbstractTranslatorTest | ||
{ | ||
public function setUp() | ||
{ | ||
$key = getenv('BING_KEY'); | ||
if (!empty($key)) { | ||
$this->translator = new BingTranslator($key); | ||
} | ||
} | ||
} |
Oops, something went wrong.