diff --git a/Classes/Event/Listener/UsageToolBarEventListener.php b/Classes/Event/Listener/UsageToolBarEventListener.php index 2c35d3ea..d925be56 100644 --- a/Classes/Event/Listener/UsageToolBarEventListener.php +++ b/Classes/Event/Listener/UsageToolBarEventListener.php @@ -49,11 +49,14 @@ public function __invoke(SystemInformationToolbarCollectorEvent $systemInformati 'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:usages.toolbar.message' ); - $severity = $this->determineSeverity($usage->character->count, $usage->character->limit); + $severity = $this->usageService->determineSeverityForSystemInformation($usage->character->count, $usage->character->limit); $systemInformation->getToolbarItem()->addSystemInformation( $title, - sprintf($message, $this->formatNumber($usage->character->count), $this->formatNumber($usage->character->limit)), + sprintf( + $message, + $this->usageService->formatNumber($usage->character->count), + $this->usageService->formatNumber($usage->character->limit)), 'actions-localize-deepl', $severity ); @@ -63,38 +66,4 @@ 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 d5116d8d..bec49272 100644 --- a/Classes/Hooks/UsageProcessAfterFinishHook.php +++ b/Classes/Hooks/UsageProcessAfterFinishHook.php @@ -4,8 +4,10 @@ namespace WebVision\WvDeepltranslate\Hooks; +use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; use TYPO3\CMS\Core\Core\Environment; use TYPO3\CMS\Core\DataHandling\DataHandler; +use TYPO3\CMS\Core\Information\Typo3Version; use TYPO3\CMS\Core\Localization\LanguageService; use TYPO3\CMS\Core\Messaging\FlashMessage; use TYPO3\CMS\Core\Messaging\FlashMessageService; @@ -46,7 +48,7 @@ public function processCmdmap_afterFinish(DataHandler $dataHandler): void 'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:usages.flashmassage.limit.description' ); - $severity = $this->determineSeverity($usage->character->count, $usage->character->limit); + $severity = $this->usageService->determineSeverity($usage->character->count, $usage->character->limit); // Reduce noise - Don't bother editors with low quota usage messages if ($severity === FlashMessage::NOTICE) { return; @@ -57,7 +59,10 @@ public function processCmdmap_afterFinish(DataHandler $dataHandler): void $flashMessage = GeneralUtility::makeInstance( FlashMessage::class, - sprintf($message, $this->formatNumber($usage->character->count), $this->formatNumber($usage->character->limit)), + sprintf( + $message, + $this->usageService->formatNumber($usage->character->count) ?: $usage->character->count, + $this->usageService->formatNumber($usage->character->limit)) ?: $usage->character->limit, $title, $severity, true @@ -70,37 +75,4 @@ 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/Classes/Service/UsageService.php b/Classes/Service/UsageService.php index 25b15658..77b6f6bc 100644 --- a/Classes/Service/UsageService.php +++ b/Classes/Service/UsageService.php @@ -5,6 +5,10 @@ namespace WebVision\WvDeepltranslate\Service; use DeepL\Usage; +use TYPO3\CMS\Backend\Toolbar\Enumeration\InformationStatus; +use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; +use TYPO3\CMS\Core\Information\Typo3Version; +use TYPO3\CMS\Core\Messaging\FlashMessage; use WebVision\WvDeepltranslate\ClientInterface; final class UsageService implements UsageServiceInterface @@ -47,4 +51,76 @@ public function isTranslateLimitExceeded(): bool } return $usage->character->count >= $usage->character->limit; } + + /** + * Make large API limits easier to read + * + * @param int $number Any large integer - 5000000 + * @return string|false Formated, better readable string variant of the integer - 5.000.000 + */ + public function formatNumber(int $number) + { + // @todo typo3/cms-core:>=12 remove polyfill switch, as php-intl is + // then a hard requirement and the polyfill is not needed + if ((new Typo3Version())->getMajorVersion() <= 12 && !extension_loaded('intl')) { + if (!class_exists(\NumberFormatter::class)) { + return number_format($number); + } + // TYPO3 v11 has a Symfony Polyfill, but this ony allows + // locale en or null, so we call with default parameter. + $numberFormatter = new \NumberFormatter('en', \NumberFormatter::DECIMAL); + } else { + $language = 'en'; + if ($this->getBackendUser() !== null) { + $uc = $this->getBackendUser()->uc; + if (is_array($uc) && array_key_exists('lang', $uc)) { + $language = $uc['lang']; + } + } + $numberFormatter = new \NumberFormatter($language, \NumberFormatter::DECIMAL); + } + return $numberFormatter->format($number); + } + + /** + * 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 + */ + public 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; + } + + public function determineSeverityForSystemInformation(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; + } + + private function getBackendUser(): ?BackendUserAuthentication + { + return $GLOBALS['BE_USER'] ?? null; + } }