diff --git a/Classes/Event/Listener/UsageToolBarEventListener.php b/Classes/Event/Listener/UsageToolBarEventListener.php index 74c0104d..2c35d3ea 100644 --- a/Classes/Event/Listener/UsageToolBarEventListener.php +++ b/Classes/Event/Listener/UsageToolBarEventListener.php @@ -7,6 +7,7 @@ use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerAwareTrait; use TYPO3\CMS\Backend\Backend\Event\SystemInformationToolbarCollectorEvent; +use TYPO3\CMS\Backend\Toolbar\Enumeration\InformationStatus; use TYPO3\CMS\Core\Localization\LanguageService; use WebVision\WvDeepltranslate\Exception\ApiKeyNotSetException; use WebVision\WvDeepltranslate\Service\UsageService; @@ -40,14 +41,21 @@ public function __invoke(SystemInformationToolbarCollectorEvent $systemInformati } return; } + + $title = $this->getLanguageService()->sL( + 'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:usages.toolbar-label' + ); + $message = $this->getLanguageService()->sL( + 'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:usages.toolbar.message' + ); + + $severity = $this->determineSeverity($usage->character->count, $usage->character->limit); + $systemInformation->getToolbarItem()->addSystemInformation( - $this->getLanguageService()->sL('LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:usages.toolbar-label'), - sprintf( - '%d / %d', - $usage->character->count, - $usage->character->limit - ), + $title, + sprintf($message, $this->formatNumber($usage->character->count), $this->formatNumber($usage->character->limit)), 'actions-localize-deepl', + $severity ); } @@ -55,4 +63,38 @@ private function getLanguageService(): LanguageService { return $GLOBALS['LANG']; } + + /** + * Make large API limits easier to read + * + * @param int $number Any large integer - 5000000 + * @return string Formated, better readable string variant of the integer - 5.000.000 + */ + private function formatNumber(int $number): string + { + return number_format($number, 0, ',', '.'); + } + + + /** + * Calculate the message severity based on the quota usage rate + * + * @param int $characterCount Already translated characters in the current billing period + * @param int $characterLimit Total character limit in the current billing period + * @return string Severity level + */ + private function determineSeverity(int $characterCount, int $characterLimit): string + { + $quotaUtilization = ($characterCount / $characterLimit) * 100; + if ($quotaUtilization >= 100) { + return InformationStatus::STATUS_ERROR; + } + if ($quotaUtilization >= 98) { + return InformationStatus::STATUS_WARNING; + } + if ($quotaUtilization >= 90) { + return InformationStatus::STATUS_INFO; + } + return InformationStatus::STATUS_NOTICE; + } } diff --git a/Classes/Hooks/UsageProcessAfterFinishHook.php b/Classes/Hooks/UsageProcessAfterFinishHook.php index 8464acd4..d5116d8d 100644 --- a/Classes/Hooks/UsageProcessAfterFinishHook.php +++ b/Classes/Hooks/UsageProcessAfterFinishHook.php @@ -39,22 +39,26 @@ public function processCmdmap_afterFinish(DataHandler $dataHandler): void return; } - $label = $this->getLanguageService()->sL( + $title = $this->getLanguageService()->sL( + 'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:usages.flashmassage.title' + ); + $message = $this->getLanguageService()->sL( 'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:usages.flashmassage.limit.description' ); + $severity = $this->determineSeverity($usage->character->count, $usage->character->limit); + // Reduce noise - Don't bother editors with low quota usage messages + if ($severity === FlashMessage::NOTICE) { + return; + } + $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class); $notificationQueue = $flashMessageService->getMessageQueueByIdentifier(); - $severity = -1; // Info - if ($this->usageService->isTranslateLimitExceeded()) { - $severity = 1; // Warning - } - $flashMessage = GeneralUtility::makeInstance( FlashMessage::class, - sprintf($label, $usage->character->count, $usage->character->limit), - 'Deepl Usage', + sprintf($message, $this->formatNumber($usage->character->count), $this->formatNumber($usage->character->limit)), + $title, $severity, true ); @@ -66,4 +70,37 @@ private function getLanguageService(): LanguageService { return $GLOBALS['LANG']; } + + /** + * Make large API limits easier to read + * + * @param int $number Any large integer - 5000000 + * @return string Formated, better readable string variant of the integer - 5.000.000 + */ + private function formatNumber(int $number): string + { + return number_format($number, 0, ',', '.'); + } + + /** + * Calculate the message severity based on the quota usage rate + * + * @param int $characterCount Already translated characters in the current billing period + * @param int $characterLimit Total character limit in the current billing period + * @return int Severity level + */ + private function determineSeverity(int $characterCount, int $characterLimit): int + { + $quotaUtilization = ($characterCount / $characterLimit) * 100; + if ($quotaUtilization >= 100) { + return FlashMessage::ERROR; + } + if ($quotaUtilization >= 98) { + return FlashMessage::WARNING; + } + if ($quotaUtilization >= 90) { + return FlashMessage::INFO; + } + return FlashMessage::NOTICE; + } } diff --git a/Configuration/Services.php b/Configuration/Services.php index 81fe737d..c4a52d80 100644 --- a/Configuration/Services.php +++ b/Configuration/Services.php @@ -174,7 +174,7 @@ 'title' => 'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:widgets.deepltranslate.widget.useswidget.title', 'description' => 'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:widgets.deepltranslate.widget.useswidget.description', 'iconIdentifier' => 'content-widget-list', - 'height' => 'medium', + 'height' => 'small', 'width' => 'small', ]) ; diff --git a/Resources/Private/Backend/Templates/Widget/UsageWidget.html b/Resources/Private/Backend/Templates/Widget/UsageWidget.html index 8261de58..6377efeb 100644 --- a/Resources/Private/Backend/Templates/Widget/UsageWidget.html +++ b/Resources/Private/Backend/Templates/Widget/UsageWidget.html @@ -8,14 +8,23 @@

{item.label}

+ + + {item.usage.count / item.usage.limit * 100} + + + + + +

- {f:translate(key: 'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:widgets.deepltranslate.widget.useswidget.count')}: {item.usage.count -> f:format.number(thousandsSeparator: '.', decimalSeparator: '', decimals: 0)} -
- {f:translate(key: 'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:widgets.deepltranslate.widget.useswidget.limit')}: {item.usage.limit -> f:format.number(thousandsSeparator: '.', decimalSeparator: '', decimals: 0)} +

- {item.usage.count / item.usage.limit * 100}
-
{item.usage.count -> f:format.number(thousandsSeparator: '.', decimalSeparator: '', decimals: 0)}
+
 
@@ -25,4 +34,4 @@

{item.label}

- \ No newline at end of file + diff --git a/Resources/Private/Language/de.locallang.xlf b/Resources/Private/Language/de.locallang.xlf index 476ba437..cf820dc5 100644 --- a/Resources/Private/Language/de.locallang.xlf +++ b/Resources/Private/Language/de.locallang.xlf @@ -199,13 +199,25 @@ - DeepL Translate Limit + DeepL Translate Quota DeepL Übersetzung Limit + + %s / %s + %s / %s + Information not available Information nicht verfügbar + + DeepL usage and quota + DeepL Nutzung und Limits + + + DeepL translations for current billing period: %s of %s characters + DeepL Übersetzungen für den aktuellen Abrechnungszeitraum: %s von %s Zeichen + @@ -213,20 +225,16 @@ DeepL Verwendung - - + Show a DeepL usage bar + Zeige einen Fortschrittsbalken für die DeepL Nutzung Character Zeichen - - Count - Zähler - - - Limit - Begrenzung + + DeepL translations for current billing period: %s of %s characters + DeepL Übersetzungen für aktuellen Abrechnungszeitraum: %s von %s Zeichen diff --git a/Resources/Private/Language/locallang.xlf b/Resources/Private/Language/locallang.xlf index 4ccdeb68..1b508cff 100644 --- a/Resources/Private/Language/locallang.xlf +++ b/Resources/Private/Language/locallang.xlf @@ -174,16 +174,19 @@ - DeepL Translate Limit + DeepL Translate Quota + + + %s / %s Information not available - DeepL Translate + DeepL usage and quota - DeepL Translate Limit %s / %s + DeepL translations for current billing period: %s of %s characters @@ -208,16 +211,13 @@ DeepL Usage - + Show a DeepL usage bar Character - - Count - - - Limit + + DeepL translations for current billing period: %s of %s characters diff --git a/Resources/Public/Icons/actions-localize-deepl.svg b/Resources/Public/Icons/actions-localize-deepl.svg index 92dd89f6..269c69c1 100644 --- a/Resources/Public/Icons/actions-localize-deepl.svg +++ b/Resources/Public/Icons/actions-localize-deepl.svg @@ -1,18 +1,9 @@ - - - - - - - + + - - +