added daterange-picker and localized date-inputs (#540)
This commit is contained in:
@@ -9,8 +9,8 @@
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use App\Utils\LocaleSettings;
|
||||
use DateTime;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
/**
|
||||
@@ -18,27 +18,17 @@ use Twig\TwigFilter;
|
||||
*/
|
||||
class DateExtensions extends \Twig_Extension
|
||||
{
|
||||
private const FALLBACK_SHORT = 'Y-m-d';
|
||||
/**
|
||||
* @var LocaleSettings
|
||||
*/
|
||||
protected $localeSettings;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
* @param LocaleSettings $localeSettings
|
||||
*/
|
||||
protected $dateSettings;
|
||||
|
||||
/**
|
||||
* @var RequestStack
|
||||
*/
|
||||
protected $requestStack;
|
||||
|
||||
/**
|
||||
* DateExtensions constructor.
|
||||
* @param RequestStack $requestStack
|
||||
* @param array $dateSettings
|
||||
*/
|
||||
public function __construct(RequestStack $requestStack, array $dateSettings)
|
||||
public function __construct(LocaleSettings $localeSettings)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
$this->dateSettings = $dateSettings;
|
||||
$this->localeSettings = $localeSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,26 +43,12 @@ class DateExtensions extends \Twig_Extension
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getLocale()
|
||||
{
|
||||
return $this->requestStack->getCurrentRequest()->getLocale();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $context
|
||||
* @param DateTime $date
|
||||
* @return string
|
||||
*/
|
||||
public function dateShort(DateTime $date)
|
||||
{
|
||||
$locale = $this->getLocale();
|
||||
$format = self::FALLBACK_SHORT;
|
||||
|
||||
if (isset($this->dateSettings[$locale]['date_short'])) {
|
||||
$format = $this->dateSettings[$locale]['date_short'];
|
||||
}
|
||||
$format = $this->localeSettings->getDateFormat();
|
||||
|
||||
return date_format($date, $format);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace App\Twig;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
use App\Utils\Duration;
|
||||
use App\Utils\LocaleSettings;
|
||||
use NumberFormatter;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Intl\Intl;
|
||||
@@ -22,9 +23,9 @@ use Twig\TwigFilter;
|
||||
class Extensions extends \Twig_Extension
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
* @var LocaleSettings
|
||||
*/
|
||||
protected $locales;
|
||||
protected $localeSettings;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
@@ -89,12 +90,12 @@ class Extensions extends \Twig_Extension
|
||||
|
||||
/**
|
||||
* @param RequestStack $requestStack
|
||||
* @param array $languages
|
||||
* @param LocaleSettings $localeSettings
|
||||
*/
|
||||
public function __construct(RequestStack $requestStack, array $languages)
|
||||
public function __construct(RequestStack $requestStack, LocaleSettings $localeSettings)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
$this->locales = $languages;
|
||||
$this->localeSettings = $localeSettings;
|
||||
$this->durationFormatter = new Duration();
|
||||
}
|
||||
|
||||
@@ -200,19 +201,8 @@ class Extensions extends \Twig_Extension
|
||||
return '?';
|
||||
}
|
||||
|
||||
$locale = $this->getLocale();
|
||||
switch ($format) {
|
||||
case 'full':
|
||||
$format = isset($this->locales[$locale]) ? $this->locales[$locale]['duration'] : null;
|
||||
break;
|
||||
case null:
|
||||
case 'short':
|
||||
$format = isset($this->locales[$locale]) ? $this->locales[$locale]['duration_short'] : null;
|
||||
break;
|
||||
}
|
||||
|
||||
if (null === $format) {
|
||||
$format = '%h:%m h';
|
||||
$format = $this->localeSettings->getDurationFormat();
|
||||
}
|
||||
|
||||
return $this->durationFormatter->format($seconds, $format);
|
||||
@@ -253,7 +243,7 @@ class Extensions extends \Twig_Extension
|
||||
*/
|
||||
public function money($amount, $currency = null)
|
||||
{
|
||||
$locale = $this->getLocale();
|
||||
$locale = $this->localeSettings->getLocale();
|
||||
|
||||
if ($this->locale !== $locale) {
|
||||
$this->locale = $locale;
|
||||
@@ -271,14 +261,6 @@ class Extensions extends \Twig_Extension
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getLocale()
|
||||
{
|
||||
return $this->requestStack->getCurrentRequest()->getLocale();
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the list of codes of the locales (languages) enabled in the
|
||||
* application and returns an array with the name of each locale written
|
||||
@@ -289,7 +271,7 @@ class Extensions extends \Twig_Extension
|
||||
public function getLocales()
|
||||
{
|
||||
$locales = [];
|
||||
foreach (array_keys($this->locales) as $locale) {
|
||||
foreach ($this->localeSettings->getAvailableLanguages() as $locale) {
|
||||
$locales[] = ['code' => $locale, 'name' => Intl::getLocaleBundle()->getLocaleName($locale, $locale)];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user