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 @@
- {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)}
- {item.label}
+
- {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)}
+