Когда я публикую список на сайте, над которым я работаю, возникает ошибка с переводом:
Request.CRITICAL: Uncaught PHP Exception Cocorico \ TranslationBundle \ Model \ Manager \ Exception>\ TranslationKeyIsInvalid: «отсутствующий ключ» в /var/www/Symfony/vendor/cocorico/cocorico/src/Cocorico>/TranslationBundle/Model/Manager/TranslatorManager.php строка 55
Как можно обойтиэта ошибка, чтобы позволить пользователям зарегистрировать новый список?
My TranslatorManager.php:
<?php
namespace Cocorico\TranslationBundle\Model\Manager;
use Cocorico\TranslationBundle\Model\Manager\Exception\TranslationKeyIsInvalid;
use Cocorico\TranslationBundle\Model\Manager\Exception\TranslationQuotaExceeded;
class TranslatorManager
{
protected $clientSecret;
protected $tokenUrl;
protected $translateUrl;
/**
* __construct
*
* @param string $clientSecret
* @param string $tokenUrl
* @param string $translateUrl
*/
public function __construct(
$clientSecret,
$tokenUrl,
$translateUrl
) {
$this->clientSecret = $clientSecret;
$this->tokenUrl = $tokenUrl;
$this->translateUrl = $translateUrl;
}
/**
* getTranslation returns translated string from the server replacing tags
*
* @param string $fromLanguage
* @param string $toLanguage
* @param array $text
* @return array
*/
public function getTranslation($fromLanguage, $toLanguage, $text = array())
{
$responseArray = array();
if (!$this->clientSecret) {
throw new TranslationKeyIsInvalid('missing key');
}
$xml = <<<XML
<TranslateArrayRequest>
<AppId/>
<From>{$fromLanguage}</From>
<Options>
<Category xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
<ContentType xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2">text/plain</ContentType>
<ReservedFlags xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
<State xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
<Uri xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
<User xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
</Options>
<Texts>
XML;
foreach ($text as $inputStr) {
$inputStr = str_ireplace('<![CDATA', '', $inputStr);
$xml .= <<<XML
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><![CDATA[{$inputStr}]]></string>
XML;
}
$xml .= <<<XML
</Texts>
<To>{$toLanguage}</To>
</TranslateArrayRequest>
XML;
$response = $this->getTranslationResponse($xml);
$xmlObj = new \SimpleXMLElement($response);
if (!isset($xmlObj->TranslateArrayResponse)) {
throw new \LogicException('Response from translator is incomplete');
}
foreach ($xmlObj->TranslateArrayResponse as $translatedArrObj) {
$responseArray[] = (string)$translatedArrObj->TranslatedText;
}
if (!count($responseArray)) {
throw new \LogicException('Empty response');
}
return $responseArray;
}
/**
* @param $requestXml
* @return mixed
* @throws \Exception
*/
protected function getTranslationResponse($requestXml)
{
$curlHandler = curl_init();
curl_setopt($curlHandler, CURLOPT_URL, $this->translateUrl);
curl_setopt(
$curlHandler,
CURLOPT_HTTPHEADER,
array('Authorization: Bearer ' . $this->getAccessToken(), 'Content-Type: text/xml')
);
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, false);
if ($requestXml) {
//Set HTTP POST Request.
curl_setopt($curlHandler, CURLOPT_POST, true);
//Set data to POST in HTTP "POST" Operation.
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $requestXml);
}
$response = curl_exec($curlHandler);
$curlErrNo = curl_errno($curlHandler);
if ($curlErrNo) {
$curlError = curl_error($curlHandler);
throw new \Exception($curlError);
}
curl_close($curlHandler);
if (preg_match('/missing subscription key/i', $response)) {
throw new TranslationKeyIsInvalid('missing key');
}
return $response;
}
protected function getAccessToken()
{
$curlHandler = curl_init();
$dataString = json_encode('{body}');
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $dataString);
curl_setopt(
$curlHandler,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Content-Length: ' . strlen($dataString),
'Ocp-Apim-Subscription-Key: ' . $this->clientSecret
)
);
curl_setopt($curlHandler, CURLOPT_URL, $this->tokenUrl);
curl_setopt($curlHandler, CURLOPT_HEADER, false);
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
$strResponse = curl_exec($curlHandler);
curl_close($curlHandler);
if (preg_match('/Out of call volume quota/i', $strResponse)) {
throw new TranslationQuotaExceeded('quota exceeded for translation');
}
if (preg_match('/invalid subscription key/i', $strResponse)) {
throw new TranslationKeyIsInvalid('your key is invalid');
}
return $strResponse;
}