示例#1
0
 public function testCurlRequestGET()
 {
     AppFileUtil::$neverCurl = false;
     AppFileUtil::$alwaysCurl = true;
     $url = Yii::app()->getUpdateServer() . '/installs/updates/updateCheck';
     $this->assertTrue(AppFileUtil::tryCurl($url));
     $response = RequestUtil::request(array('url' => $url, 'method' => 'GET'));
     $this->assertEquals(1, preg_match('/^\\d(\\.\\d)*$/', $response));
 }
示例#2
0
 /**
  * Translate a message via Google Translate API.
  *
  * Helper function called by {@link updateTranslations}
  * to translate individual messages via the Google Translate API. Any text
  * between braces {} is preserved as is for variable replacement.
  *
  * @param string $message The untranslated message
  * @param string $lang The language to translate to
  * @return string The translated message
  */
 public function translateMessage($message, $lang)
 {
     $this->verbose && (print " Translating {$message} to {$lang}\n");
     $key = (require Yii::app()->basePath . '/config/googleApiKey.php');
     // Git Ignored file containing the Google API key to store. Ours is not included with public release for security reasons...
     $message = $this->addNoTranslateTags($message);
     $this->characterCount += mb_strlen($message, 'UTF-8');
     $params = array('key' => $key, 'source' => 'en', 'target' => $lang, 'q' => $message);
     $url = 'https://www.googleapis.com/language/translate/v2?' . http_build_query($params);
     $data = RequestUtil::request(array('url' => $url, 'method' => 'GET'));
     $data = json_decode($data, true);
     // Response is JSON, need to decode it to an array.
     if (isset($data['data'], $data['data']['translations'], $data['data']['translations'][0], $data['data']['translations'][0]['translatedText'])) {
         $message = $data['data']['translations'][0]['translatedText'];
         // Make sure the data structure returned is correct, then store the message as the translated version.
     } else {
         $message = '';
         // Otherwise, leave the message blank.
     }
     $message = $this->removeNoTranslateTags($message);
     $message = trim($message, '\\/');
     // Trim any harmful characters Google Translate may have moved around, like leaving a "\" at the end of the string...
     return $message;
 }