|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace RZ\Roadiz\RozierBundle\TranslateAssistant; |
| 6 | + |
| 7 | +use DeepL\DeepLClient; |
| 8 | +use DeepL\DeepLException; |
| 9 | +use DeepL\Language; |
| 10 | +use Psr\Cache\CacheItemPoolInterface; |
| 11 | +use Psr\Cache\InvalidArgumentException; |
| 12 | + |
| 13 | +final readonly class DeeplTranslateAssistant implements TranslateAssistantInterface |
| 14 | +{ |
| 15 | + public function __construct( |
| 16 | + private CacheItemPoolInterface $cache, |
| 17 | + private string $apiKey, |
| 18 | + ) { |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * @throws DeepLException |
| 23 | + * @throws InvalidArgumentException |
| 24 | + */ |
| 25 | + #[\Override] |
| 26 | + public function translate(TranslateAssistantInput $translatorDto): TranslateAssistantOutput |
| 27 | + { |
| 28 | + if (empty($this->apiKey)) { |
| 29 | + throw new DeepLException('DeepL api key is required.'); |
| 30 | + } |
| 31 | + |
| 32 | + $deeplClient = new DeepLClient($this->apiKey); |
| 33 | + |
| 34 | + $this->denyNotAvailableLanguages($this->transformTargetLang($translatorDto->targetLang), $deeplClient, 'translate'); |
| 35 | + |
| 36 | + $result = $deeplClient->translateText($translatorDto->text, $translatorDto->sourceLang, $this->transformTargetLang($translatorDto->targetLang), $translatorDto->options); |
| 37 | + |
| 38 | + if (is_array($result)) { |
| 39 | + $result = $result[0]; |
| 40 | + } |
| 41 | + |
| 42 | + return new TranslateAssistantOutput( |
| 43 | + originalText: $translatorDto->text, |
| 44 | + translatedText: $result->text, |
| 45 | + sourceLang: $result->detectedSourceLang, |
| 46 | + targetLang: $translatorDto->targetLang, |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * This feature requires a PRO Deepl Api-token. |
| 52 | + * https://developers.deepl.com/api-reference/improve-text/deepl-write-api-service-specification-updates. |
| 53 | + * |
| 54 | + * @throws DeepLException|InvalidArgumentException |
| 55 | + */ |
| 56 | + #[\Override] |
| 57 | + public function rephrase(TranslateAssistantInput $translatorDto): TranslateAssistantOutput |
| 58 | + { |
| 59 | + if (empty($this->apiKey)) { |
| 60 | + throw new DeepLException('DeepL api key is required.'); |
| 61 | + } |
| 62 | + |
| 63 | + $deeplClient = new DeepLClient($this->apiKey); |
| 64 | + |
| 65 | + $this->denyNotAvailableLanguages($translatorDto->targetLang, $deeplClient, 'rephrase'); |
| 66 | + |
| 67 | + $result = $deeplClient->rephraseText($translatorDto->text, $this->transformTargetLang($translatorDto->targetLang), $translatorDto->options); |
| 68 | + |
| 69 | + return new TranslateAssistantOutput( |
| 70 | + originalText: $translatorDto->text, |
| 71 | + translatedText: is_array($result) ? $result[0]->text : $result->text, |
| 72 | + sourceLang: $translatorDto->sourceLang, |
| 73 | + targetLang: $translatorDto->targetLang, |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + private function transformTargetLang(string $targetLang): string |
| 78 | + { |
| 79 | + return match ($targetLang) { |
| 80 | + 'en' => 'en-GB', |
| 81 | + 'pt' => 'pt-PT', |
| 82 | + default => $targetLang, |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @throws DeepLException |
| 88 | + * @throws InvalidArgumentException |
| 89 | + */ |
| 90 | + private function denyNotAvailableLanguages(string $targetLanguage, DeepLClient $deeplClient, string $method): void |
| 91 | + { |
| 92 | + $languageAvailableCacheItem = $this->cache->getItem('DeeplTranslateAssistant_targetLanguages_'.$method.'_'.$targetLanguage); |
| 93 | + |
| 94 | + if (!$languageAvailableCacheItem->isHit()) { |
| 95 | + $languageAvailableCacheItem->set( |
| 96 | + in_array( |
| 97 | + mb_strtoupper($this->transformTargetLang($targetLanguage)), |
| 98 | + array_map(fn (Language $language) => $language->code, $deeplClient->getTargetLanguages()) |
| 99 | + ) |
| 100 | + ); |
| 101 | + $languageAvailableCacheItem->expiresAfter(3600); |
| 102 | + $this->cache->save($languageAvailableCacheItem); |
| 103 | + } |
| 104 | + |
| 105 | + if (!$languageAvailableCacheItem->get()) { |
| 106 | + throw new DeepLException('Invalid target language.'); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + #[\Override] |
| 111 | + public function supportRephrase(): bool |
| 112 | + { |
| 113 | + return !str_ends_with($this->apiKey, ':fx'); |
| 114 | + } |
| 115 | +} |
0 commit comments