translate time-tracking in login screen and browser title (#738)
This commit is contained in:
98
src/Twig/DatatableExtensions.php
Normal file
98
src/Twig/DatatableExtensions.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class DatatableExtensions extends AbstractExtension
|
||||
{
|
||||
/**
|
||||
* @var RequestStack
|
||||
*/
|
||||
protected $requestStack;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $cookies = [];
|
||||
|
||||
/**
|
||||
* @param RequestStack $requestStack
|
||||
*/
|
||||
public function __construct(RequestStack $requestStack)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('is_visible_column', [$this, 'isColumnVisible']),
|
||||
new TwigFunction('is_datatable_configured', [$this, 'isDatatableConfigured']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dataTable
|
||||
* @return bool
|
||||
*/
|
||||
public function isDatatableConfigured(string $dataTable)
|
||||
{
|
||||
$cookie = $this->getVisibilityCookieName($dataTable);
|
||||
|
||||
return $this->requestStack->getCurrentRequest()->cookies->has($cookie);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dataTable
|
||||
* @return string
|
||||
*/
|
||||
protected function getVisibilityCookieName(string $dataTable)
|
||||
{
|
||||
return $dataTable . '_visibility';
|
||||
}
|
||||
|
||||
/**
|
||||
* This is only for datatables, do not use it outside this context.
|
||||
*
|
||||
* @param string $dataTable
|
||||
* @param string $column
|
||||
* @return bool
|
||||
*/
|
||||
public function isColumnVisible(string $dataTable, string $column)
|
||||
{
|
||||
// name handling is spread between here and datatables.html.twig (data_table_column_modal)
|
||||
$cookie = $this->getVisibilityCookieName($dataTable);
|
||||
|
||||
if (!isset($this->cookies[$cookie])) {
|
||||
$visibility = false;
|
||||
if ($this->requestStack->getCurrentRequest()->cookies->has($cookie)) {
|
||||
$visibility = json_decode($this->requestStack->getCurrentRequest()->cookies->get($cookie), true);
|
||||
}
|
||||
$this->cookies[$cookie] = $visibility;
|
||||
}
|
||||
$values = $this->cookies[$cookie];
|
||||
|
||||
if (empty($values) || !is_array($values)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isset($values[$column]) && $values[$column] === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,6 @@ use App\Entity\Timesheet;
|
||||
use App\Utils\Duration;
|
||||
use App\Utils\LocaleSettings;
|
||||
use NumberFormatter;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Intl\Intl;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
@@ -29,17 +28,14 @@ class Extensions extends AbstractExtension
|
||||
* @var LocaleSettings
|
||||
*/
|
||||
protected $localeSettings;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $locale;
|
||||
|
||||
/**
|
||||
* @var Duration
|
||||
*/
|
||||
protected $durationFormatter;
|
||||
|
||||
/**
|
||||
* @var NumberFormatter
|
||||
*/
|
||||
@@ -49,16 +45,6 @@ class Extensions extends AbstractExtension
|
||||
*/
|
||||
protected $moneyFormatter;
|
||||
|
||||
/**
|
||||
* @var RequestStack
|
||||
*/
|
||||
protected $requestStack;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $cookies = [];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
@@ -88,7 +74,7 @@ class Extensions extends AbstractExtension
|
||||
'start-small' => 'fas fa-play-circle',
|
||||
'stop' => 'fas fa-stop',
|
||||
'stop-small' => 'far fa-stop-circle',
|
||||
'timesheet' => 'far fa-clock',
|
||||
'timesheet' => 'fas fa-clock',
|
||||
'trash' => 'far fa-trash-alt',
|
||||
'user' => 'fas fa-user',
|
||||
'visibility' => 'far fa-eye',
|
||||
@@ -110,12 +96,10 @@ class Extensions extends AbstractExtension
|
||||
];
|
||||
|
||||
/**
|
||||
* @param RequestStack $requestStack
|
||||
* @param LocaleSettings $localeSettings
|
||||
*/
|
||||
public function __construct(RequestStack $requestStack, LocaleSettings $localeSettings)
|
||||
public function __construct(LocaleSettings $localeSettings)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
$this->localeSettings = $localeSettings;
|
||||
$this->durationFormatter = new Duration();
|
||||
}
|
||||
@@ -142,8 +126,6 @@ class Extensions extends AbstractExtension
|
||||
{
|
||||
return [
|
||||
new TwigFunction('locales', [$this, 'getLocales']),
|
||||
new TwigFunction('is_visible_column', [$this, 'isColumnVisible']),
|
||||
new TwigFunction('is_datatable_configured', [$this, 'isDatatableConfigured']),
|
||||
new TwigFunction('class_name', [$this, 'getClassName']),
|
||||
];
|
||||
}
|
||||
@@ -161,61 +143,6 @@ class Extensions extends AbstractExtension
|
||||
return get_class($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dataTable
|
||||
* @param string $size
|
||||
* @return bool
|
||||
*/
|
||||
public function isDatatableConfigured(string $dataTable, string $size)
|
||||
{
|
||||
$cookie = $this->getVisibilityCookieName($dataTable, $size);
|
||||
|
||||
return $this->requestStack->getCurrentRequest()->cookies->has($cookie);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dataTable
|
||||
* @param string $size
|
||||
* @return string
|
||||
*/
|
||||
public function getVisibilityCookieName(string $dataTable, string $size)
|
||||
{
|
||||
return $dataTable . '_visibility' . $size;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is only for datatables, do not use it outside this context.
|
||||
*
|
||||
* @param string $dataTable
|
||||
* @param string $column
|
||||
* @param string $size
|
||||
* @return bool
|
||||
*/
|
||||
public function isColumnVisible(string $dataTable, string $column, string $size)
|
||||
{
|
||||
// name handling is spread between here and datatables.html.twig (data_table_column_modal)
|
||||
$cookie = $this->getVisibilityCookieName($dataTable, $size);
|
||||
|
||||
if (!isset($this->cookies[$cookie])) {
|
||||
$visibility = false;
|
||||
if ($this->requestStack->getCurrentRequest()->cookies->has($cookie)) {
|
||||
$visibility = json_decode($this->requestStack->getCurrentRequest()->cookies->get($cookie), true);
|
||||
}
|
||||
$this->cookies[$cookie] = $visibility;
|
||||
}
|
||||
$values = $this->cookies[$cookie];
|
||||
|
||||
if (empty($values) || !is_array($values)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isset($values[$column]) && $values[$column] === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms seconds into a duration string.
|
||||
*
|
||||
|
||||
57
src/Twig/TitleExtension.php
Normal file
57
src/Twig/TitleExtension.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use App\Constants;
|
||||
use App\Entity\Timesheet;
|
||||
use App\Utils\Duration;
|
||||
use App\Utils\LocaleSettings;
|
||||
use NumberFormatter;
|
||||
use Symfony\Component\Intl\Intl;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class TitleExtension extends AbstractExtension
|
||||
{
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
* @param TranslatorInterface $translator
|
||||
*/
|
||||
public function __construct(TranslatorInterface $translator)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('get_title', [$this, 'generateTitle']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null|string $prefix
|
||||
* @param string $delimiter
|
||||
* @return string
|
||||
*/
|
||||
public function generateTitle(?string $prefix = null, string $delimiter = ' – ')
|
||||
{
|
||||
return ($prefix ?? '') . 'Kimai' . $delimiter . $this->translator->trans('time_tracking', [], 'messages');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user