/**
  * @param $language
  * @param $expectedLanguage
  *
  * @dataProvider dataSetGetLanguage
  */
 public function testSetGetTranslateFrom($language, $expectedLanguage)
 {
     $dictionaryClient = new DictionaryClient('');
     $dictionaryClient->setTranslateFrom($language);
     $this->assertEquals($expectedLanguage, $dictionaryClient->getTranslateFrom());
 }
 /**
  * 
  *
  * @param string $translateFrom
  * @return $this 
  * @static 
  */
 public static function setTranslateFrom($translateFrom)
 {
     return \Yandex\Dictionary\DictionaryClient::setTranslateFrom($translateFrom);
 }
Exemplo n.º 3
0
use Yandex\Dictionary\DictionaryClient;
use Yandex\Common\Exception\ForbiddenException;
use Yandex\Dictionary\Exception\DictionaryException;
$errorMessage = false;
// Is auth
if (isset($_COOKIE['yaAccessToken']) && isset($_COOKIE['yaClientId'])) {
    if (!isset($settings["dictionary"]["key"]) || !$settings["dictionary"]["key"]) {
        throw new DictionaryException('Empty dictionary key. Get key from https://tech.yandex.ru/keys/get/?service=dict');
    }
    $dictionaryClient = new DictionaryClient($settings["dictionary"]["key"]);
    if (isset($_POST['word']) && $_POST['word'] && isset($_POST['language']) && $_POST['language']) {
        $translation = explode('-', $_POST['language']);
        if (count($translation) === 2) {
            $from = $translation[0];
            $to = $translation[1];
            $dictionaryClient->setTranslateFrom($from)->setTranslateTo($to);
            $result = $dictionaryClient->lookup($_POST['word']);
            if ($result) {
                /** @var \Yandex\Dictionary\DictionaryDefinition $dictionaryDefinition */
                $dictionaryDefinition = $result[0];
                $dictionaryTranslation = $dictionaryDefinition->getTranslations();
                /** @var \Yandex\Dictionary\DictionaryTranslation $dictionaryTranslation */
                $dictionaryTranslation = $dictionaryDefinition->getTranslations()[0];
                $word = $dictionaryTranslation->getText();
            }
        }
    }
    try {
        $languages = $dictionaryClient->getLanguages();
    } catch (ForbiddenException $ex) {
        $errorMessage = $ex->getMessage();