From 4fa73d65e9531862c10fa6c0eea84dd90c988591 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 5 Jun 2024 14:39:33 +0200 Subject: [PATCH] wip #6945 @0.5 --- src/Api.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Api.php b/src/Api.php index e7123ce..e73857a 100644 --- a/src/Api.php +++ b/src/Api.php @@ -1,5 +1,6 @@ _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' @@ -70,4 +88,9 @@ class Api 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 -- 2.39.5