From b07c3277fa7b9be84f425292c30d99643fbe436a Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 5 Jun 2024 16:04:45 +0200 Subject: [PATCH] wip #6945 @0.5 --- .idea/cubist_azuretranslation.iml | 30 ++++++++++++++++++++++++++- .idea/php.xml | 34 +++++++++++++++++++++++++++++++ src/Api.php | 22 ++++++++++++++------ 3 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 .idea/php.xml diff --git a/.idea/cubist_azuretranslation.iml b/.idea/cubist_azuretranslation.iml index d6ebd48..37eb4f2 100644 --- a/.idea/cubist_azuretranslation.iml +++ b/.idea/cubist_azuretranslation.iml @@ -2,7 +2,35 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..4e9985e --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Api.php b/src/Api.php index e73857a..5b00e12 100644 --- a/src/Api.php +++ b/src/Api.php @@ -55,17 +55,27 @@ class Api return file_get_contents($cacheFile); } $res = $this->_translate($text, $toLanguage, $fromLanguage); - file_put_contents($cacheFile, $res); + if ($res) { + 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' - ], json_encode([['Text' => $text]])); - $data = json_decode($res->getBody(), true); - return $data[0]['translations'][0]['text']; + try { + $res = $this->_call('/translate?api-version=3.0&from=' . $fromLanguage . '&to=' . $toLanguage, 'post', [ + 'Content-type' => 'application/json' + ], json_encode([['Text' => $text]])); + $data = json_decode($res->getBody(), true); + return $data[0]['translations'][0]['text']; + } catch (\Exception $e) { + if (stristr($e->getMessage(), '429001')) { + sleep(60); + return $this->_translate($text, $toLanguage, $fromLanguage); + } + } + return false; } -- 2.39.5