<?php
+namespace Cubist\Azure\Translate;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
*/
protected $token = null;
+ protected static $_cache = null;
+
/**
* @var int
*/
}
public function translate($text, $toLanguage, $fromLanguage)
+ {
+ if (null === static::$_cache) {
+ return $this->_translate($text, $toLanguage, $fromLanguage);
+ }
+ $key = $fromLanguage . '-' . $toLanguage . '-' . hash('sha256', $text);
+ $cacheFile = static::$_cache . '/' . $key;
+ if (file_exists($cacheFile)) {
+ return file_get_contents($cacheFile);
+ }
+ $res = $this->_translate($text, $toLanguage, $fromLanguage);
+ file_put_contents($cacheFile, $res);
+ return $res;
+ }
+
+ protected function _translate($text, $toLanguage, $fromLanguage)
{
$res = $this->_call('/translate?api-version=3.0&from=' . $fromLanguage . '&to=' . $toLanguage, 'post', [
'Content-type' => 'application/json'
return $this->client->send(new Request($method, $baseURL . ltrim($url, '/'), $headers, $body), $options);
}
+
+ public static function setCache($cache)
+ {
+ self::$_cache = $cache;
+ }
}
\ No newline at end of file