Skip to content

Commit

Permalink
[TASK] Create deepl usage widget
Browse files Browse the repository at this point in the history
This widget shows the current character limit of the DeeoL Translate function, as a TYPO3 dashboard widget.
  • Loading branch information
NarkNiro committed Aug 29, 2024
1 parent dea7d0d commit a78d40c
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 0 deletions.
76 changes: 76 additions & 0 deletions Classes/Widgets/UsageWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace WebVision\WvDeepltranslate\Widgets;

use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Dashboard\Widgets\WidgetConfigurationInterface;
use TYPO3\CMS\Dashboard\Widgets\WidgetInterface;
use TYPO3\CMS\Fluid\View\StandaloneView;
use WebVision\WvDeepltranslate\Service\UsageService;

class UsageWidget implements WidgetInterface
{
private WidgetConfigurationInterface $configuration;

private StandaloneView $view;

/**
* @var array<string, mixed>
*/
private array $options;

/**
* @param array<string, mixed> $options
*/
public function __construct(
WidgetConfigurationInterface $configuration,
StandaloneView $view,
array $options = []
) {
$this->configuration = $configuration;
$this->view = $view;
$this->options = $options;
}

public function renderWidgetContent(): string
{
/** @var UsageService $usageService */
$usageService = GeneralUtility::makeInstance(UsageService::class);

// Workaround to make the widget template available in two TYPO3 versions
$templateRootPaths = $this->view->getTemplateRootPaths();
$templateRootPaths[1718368476557] = 'EXT:wv_deepltranslate/Resources/Private/Backend/Templates/';
$this->view->setTemplateRootPaths($templateRootPaths);

$currentUsage = $usageService->getCurrentUsage();

$this->view->assignMultiple([
'usages' => [
[
'label' => $this->getLanguageService()->sL('LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:widgets.deepltranslate.widget.useswidget.character'),
'usage' => $currentUsage !== null ? $currentUsage->character : [],
],
],
'options' => $this->options,
'configuration' => $this->configuration,
]);

return $this->view->render('Widget/UsageWidget');
}

/**
* @return array<string, mixed>
*/
public function getOptions(): array
{
return $this->options;
}

protected function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}
}
13 changes: 13 additions & 0 deletions Configuration/Backend/DashboardWidgetGroups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return (static function () {
if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('dashboard')) {
return [
'deepl' => [
'title' => 'DeepL Widget',
],
];
}

return [];
})();
17 changes: 17 additions & 0 deletions Configuration/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use TYPO3\CMS\Backend\Backend\Event\SystemInformationToolbarCollectorEvent;
use TYPO3\CMS\Backend\Template\Components\ModifyButtonBarEvent;
use TYPO3\CMS\Core\Cache\CacheManager;
Expand All @@ -27,6 +28,7 @@
use WebVision\WvDeepltranslate\Service\IconOverlayGenerator;
use WebVision\WvDeepltranslate\Service\LanguageService;
use WebVision\WvDeepltranslate\Service\UsageService;
use WebVision\WvDeepltranslate\Widgets\UsageWidget;

return function (ContainerConfigurator $containerConfigurator, ContainerBuilder $containerBuilder) {
$typo3version = new Typo3Version();
Expand Down Expand Up @@ -149,4 +151,19 @@
'event' => SystemInformationToolbarCollectorEvent::class,
]
);

$services->set('widgets.deepltranslate.widget.useswidget')
->class(UsageWidget::class)
->arg('$view', new Reference('dashboard.views.widget'))
->arg('$options', [])
->tag('dashboard.widget', [
'identifier' => 'widgets-deepl-uses',
'groupNames' => 'deepl',
'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',
'width' => 'small',
])
;
};
28 changes: 28 additions & 0 deletions Resources/Private/Backend/Templates/Widget/UsageWidget.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<html
data-namespace-typo3-fluid="true"
xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
>
<f:layout name="Widget/Widget" />

<f:section name="main">
<f:for as="item" each="{usages}" >
<div class="mb-3">
<h4>{item.label}</h4>
<p>
<b>{f:translate(key: 'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:widgets.deepltranslate.widget.useswidget.count')}</b>: {item.usage.count -> f:format.number(thousandsSeparator: '.', decimalSeparator: '', decimals: 0)}
<br>
<b>{f:translate(key: 'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:widgets.deepltranslate.widget.useswidget.limit')}</b>: {item.usage.limit -> f:format.number(thousandsSeparator: '.', decimalSeparator: '', decimals: 0)}
</p>
<f:variable name="procss">{item.usage.count / item.usage.limit * 100}</f:variable>
<div class="progress" role="progressbar" aria-label="Usage" aria-valuenow="{procss}" aria-valuemin="0" aria-valuemax="100" style="height: 50%">
<div class="progress-bar" style="width: {procss}%">{item.usage.count -> f:format.number(thousandsSeparator: '.', decimalSeparator: '', decimals: 0)}</div>
</div>
</div>
</f:for>
</f:section>

<f:section name="footer">
<f:render partial="Widget/Button" arguments="{button: button}"/>
</f:section>

</html>
22 changes: 22 additions & 0 deletions Resources/Private/Language/de.locallang.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,28 @@
<source>Information not available</source>
<target>Information nicht verfügbar</target>
</trans-unit>

<!-- Widget -->
<trans-unit id="widgets.deepltranslate.widget.useswidget.title">
<source>DeepL Usage</source>
<target>DeepL Verwendung</target>
</trans-unit>
<trans-unit id="widgets.deepltranslate.widget.useswidget.description">
<source></source>
<target></target>
</trans-unit>
<trans-unit id="widgets.deepltranslate.widget.useswidget.character">
<source>Character</source>
<target>Zeichen</target>
</trans-unit>
<trans-unit id="widgets.deepltranslate.widget.useswidget.count">
<source>Count</source>
<target>Zähler</target>
</trans-unit>
<trans-unit id="widgets.deepltranslate.widget.useswidget.limit">
<source>Limit</source>
<target>Begrenzung</target>
</trans-unit>
</body>
</file>
</xliff>
17 changes: 17 additions & 0 deletions Resources/Private/Language/locallang.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,23 @@
<trans-unit id="usages.flashmassage.limit.description">
<source>DeepL Translate Limit %s / %s</source>
</trans-unit>

<!-- Widget -->
<trans-unit id="widgets.deepltranslate.widget.useswidget.title">
<source>DeepL Usage</source>
</trans-unit>
<trans-unit id="widgets.deepltranslate.widget.useswidget.description">
<source></source>
</trans-unit>
<trans-unit id="widgets.deepltranslate.widget.useswidget.character">
<source>Character</source>
</trans-unit>
<trans-unit id="widgets.deepltranslate.widget.useswidget.count">
<source>Count</source>
</trans-unit>
<trans-unit id="widgets.deepltranslate.widget.useswidget.limit">
<source>Limit</source>
</trans-unit>
</body>
</file>
</xliff>
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"ramsey/uuid": "^4.2",
"saschaegerer/phpstan-typo3": "^1.9",
"typo3/cms-belog": "^11.5 || ^12.4",
"typo3/cms-dashboard": "^11.5 || ^12.4",
"typo3/cms-extensionmanager": "^11.5 || ^12.4",
"typo3/cms-filelist": "^11.5 || ^12.4",
"typo3/cms-fluid-styled-content": "^11.5 || ^12.4",
Expand Down

0 comments on commit a78d40c

Please sign in to comment.