/**
  * A method to do Google translate.
  *
  * @param   string $text      String to translate.
  * @param   string $SourceLan Translate from this language, eg: 'zh-tw'. Empty will auto detect.
  * @param   string $ResultLan Translate to this language, eg: 'en'. Empty will auto detect.
  *
  * @return  string|bool Translated text.
  */
 public static function gTranslate($text, $SourceLan, $ResultLan)
 {
     $url = new \JUri();
     // For Google APIv2
     $url->setHost('https://www.googleapis.com/');
     $url->setPath('language/translate/v2');
     $query['key'] = self::APT_KEY;
     $query['q'] = urlencode($text);
     $query['source'] = $SourceLan;
     $query['target'] = $ResultLan;
     if (!$text) {
         return false;
     }
     $url->setQuery($query);
     $url->toString();
     $response = CurlHelper::get((string) $url);
     if (empty($response->body)) {
         return '';
     }
     $json = new \JRegistry();
     $json->loadString($response->body, 'json');
     $r = $json->get('data.translations');
     return $r[0]->translatedText;
 }
Example #2
0
 /**
  * Get the origin image path, if is a remote image, will store in temp dir first.
  *
  * @param   string $url  The image URL.
  * @param   string $hash Not available now..
  *
  * @return  string  Image path.
  */
 public function getImagePath($url, $hash = null)
 {
     $self = \JUri::getInstance();
     $url = new \JUri($url);
     // Is same host?
     if ($self->getHost() == $url->getHost()) {
         $url = $url->toString();
         $path = str_replace(\JURI::root(), JPATH_ROOT . '/', $url);
         $path = \JPath::clean($path);
     } elseif (!$url->getHost()) {
         $url = $url->toString();
         $path = \JPath::clean(JPATH_ROOT . '/' . $url);
     } else {
         $handler = $this->hashHandler;
         $path = $this->config['path.temp'] . '/' . $handler(basename($url)) . '.jpg';
         if (!is_file($path)) {
             CurlHelper::download((string) $url, $path);
         }
     }
     return $path;
 }
 /**
  * Method to test download().
  *
  * @return void
  *
  * @covers Windwalker\Helper\CurlHelper::download
  */
 public function testDownload()
 {
     ini_set('allow_url_fopen', true);
     // We use static file README.md as our test file.
     $fileName = 'README.md';
     $testFileName = 'test' . $fileName;
     $filePath = __DIR__ . '/../../' . $fileName;
     $testFilePath = __DIR__ . '/../../' . $testFileName;
     $filePath = str_replace('\\', '/', $filePath);
     // Read file on local
     $oriFileContent = file_get_contents($filePath);
     // Download file by CurlHelper
     $success = CurlHelper::download('file://' . $filePath, $testFileName);
     if (!isset($success->errorCode)) {
         // Read the downloaded file
         $downloadFileContent = file_get_contents($fileName);
         // Assert two file
         $this->assertEquals($oriFileContent, $downloadFileContent);
         // Remove temporary downloaded file.
         unlink($testFilePath);
     } else {
         $this->fail(sprintf('Download: %s fail', $filePath));
     }
 }