invoices: unified money, number and date formats and fully respect configured language (#1814)
This commit is contained in:
@@ -9,8 +9,11 @@
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use App\Utils\LocaleSettings;
|
||||
use App\Configuration\LanguageFormattings;
|
||||
use App\Constants;
|
||||
use App\Utils\LocaleFormats;
|
||||
use DateTime;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
@@ -21,9 +24,9 @@ use Twig\TwigFunction;
|
||||
class DateExtensions extends AbstractExtension
|
||||
{
|
||||
/**
|
||||
* @var LocaleSettings|null
|
||||
* @var LocaleFormats|null
|
||||
*/
|
||||
protected $localeSettings = null;
|
||||
protected $localeFormats = null;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
@@ -44,13 +47,26 @@ class DateExtensions extends AbstractExtension
|
||||
* @var bool
|
||||
*/
|
||||
protected $isTwentyFourHour = null;
|
||||
|
||||
/**
|
||||
* @param LocaleSettings $localeSettings
|
||||
* @var string
|
||||
*/
|
||||
public function __construct(LocaleSettings $localeSettings)
|
||||
private $locale;
|
||||
/**
|
||||
* @var LanguageFormattings
|
||||
*/
|
||||
private $formats;
|
||||
|
||||
public function __construct(RequestStack $requestStack, LanguageFormattings $formats)
|
||||
{
|
||||
$this->localeSettings = $localeSettings;
|
||||
$locale = Constants::DEFAULT_LOCALE;
|
||||
|
||||
// request is null in a console command
|
||||
if (null !== $requestStack->getMasterRequest()) {
|
||||
$locale = $requestStack->getMasterRequest()->getLocale();
|
||||
}
|
||||
|
||||
$this->formats = $formats;
|
||||
$this->setLocale($locale);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,6 +76,7 @@ class DateExtensions extends AbstractExtension
|
||||
{
|
||||
return [
|
||||
new TwigFilter('month_name', [$this, 'monthName']),
|
||||
new TwigFilter('day_name', [$this, 'dayName']),
|
||||
new TwigFilter('date_short', [$this, 'dateShort']),
|
||||
new TwigFilter('date_time', [$this, 'dateTime']),
|
||||
new TwigFilter('date_full', [$this, 'dateTimeFull']),
|
||||
@@ -79,15 +96,25 @@ class DateExtensions extends AbstractExtension
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows to switch the locale used for all twig filter and functions.
|
||||
*
|
||||
* @param string $locale
|
||||
*/
|
||||
public function setLocale(string $locale)
|
||||
{
|
||||
$this->locale = $locale;
|
||||
$this->localeFormats = new LocaleFormats($this->formats, $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DateTime|string $date
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function dateShort($date)
|
||||
{
|
||||
if (null === $this->dateFormat) {
|
||||
$this->dateFormat = $this->localeSettings->getDateFormat();
|
||||
$this->dateFormat = $this->localeFormats->getDateFormat();
|
||||
}
|
||||
|
||||
if (!$date instanceof DateTime) {
|
||||
@@ -98,18 +125,17 @@ class DateExtensions extends AbstractExtension
|
||||
}
|
||||
}
|
||||
|
||||
return date_format($date, $this->dateFormat);
|
||||
return $date->format($this->dateFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DateTime|string $date
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function dateTime($date)
|
||||
{
|
||||
if (null === $this->dateTimeFormat) {
|
||||
$this->dateTimeFormat = $this->localeSettings->getDateTimeFormat();
|
||||
$this->dateTimeFormat = $this->localeFormats->getDateTimeFormat();
|
||||
}
|
||||
|
||||
if (!$date instanceof DateTime) {
|
||||
@@ -125,14 +151,12 @@ class DateExtensions extends AbstractExtension
|
||||
|
||||
/**
|
||||
* @param DateTime|string $date
|
||||
* @param bool $userTimezone
|
||||
* @return bool|false|string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function dateTimeFull($date, bool $userTimezone = true)
|
||||
public function dateTimeFull($date)
|
||||
{
|
||||
if (null === $this->dateTimeTypeFormat) {
|
||||
$this->dateTimeTypeFormat = $this->localeSettings->getDateTimeTypeFormat();
|
||||
$this->dateTimeTypeFormat = $this->localeFormats->getDateTimeTypeFormat();
|
||||
}
|
||||
|
||||
if (!$date instanceof DateTime) {
|
||||
@@ -143,17 +167,11 @@ class DateExtensions extends AbstractExtension
|
||||
}
|
||||
}
|
||||
|
||||
$timezone = date_default_timezone_get();
|
||||
|
||||
if (!$userTimezone) {
|
||||
$timezone = $date->getTimezone()->getName();
|
||||
}
|
||||
|
||||
$formatter = new \IntlDateFormatter(
|
||||
$this->localeSettings->getLocale(),
|
||||
$this->locale,
|
||||
\IntlDateFormatter::MEDIUM,
|
||||
\IntlDateFormatter::MEDIUM,
|
||||
$timezone,
|
||||
date_default_timezone_get(),
|
||||
\IntlDateFormatter::GREGORIAN,
|
||||
$this->dateTimeTypeFormat
|
||||
);
|
||||
@@ -177,34 +195,52 @@ class DateExtensions extends AbstractExtension
|
||||
}
|
||||
}
|
||||
|
||||
return date_format($date, $format);
|
||||
return $date->format($format);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DateTime|string $date
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function time($date)
|
||||
{
|
||||
if (null === $this->timeFormat) {
|
||||
$this->timeFormat = $this->localeSettings->getTimeFormat();
|
||||
$this->timeFormat = $this->localeFormats->getTimeFormat();
|
||||
}
|
||||
|
||||
if (!$date instanceof DateTime) {
|
||||
$date = new DateTime($date);
|
||||
}
|
||||
|
||||
return date_format($date, $this->timeFormat);
|
||||
return $date->format($this->timeFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime $date
|
||||
* @return string
|
||||
*/
|
||||
public function monthName(\DateTime $date)
|
||||
public function monthName(\DateTime $dateTime): string
|
||||
{
|
||||
return 'month.' . $date->format('n');
|
||||
$formatter = new \IntlDateFormatter(
|
||||
$this->locale,
|
||||
\IntlDateFormatter::FULL,
|
||||
\IntlDateFormatter::FULL,
|
||||
$dateTime->getTimezone()->getName(),
|
||||
\IntlDateFormatter::GREGORIAN,
|
||||
'LLLL'
|
||||
);
|
||||
|
||||
return $formatter->format($dateTime);
|
||||
}
|
||||
|
||||
public function dayName(\DateTime $dateTime): string
|
||||
{
|
||||
$formatter = new \IntlDateFormatter(
|
||||
$this->locale,
|
||||
\IntlDateFormatter::FULL,
|
||||
\IntlDateFormatter::FULL,
|
||||
$dateTime->getTimezone()->getName(),
|
||||
\IntlDateFormatter::GREGORIAN,
|
||||
'EEEE'
|
||||
);
|
||||
|
||||
return $formatter->format($dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -215,7 +251,7 @@ class DateExtensions extends AbstractExtension
|
||||
public function hour24($twentyFour, $twelveHour)
|
||||
{
|
||||
if (null === $this->isTwentyFourHour) {
|
||||
$this->isTwentyFourHour = $this->localeSettings->isTwentyFourHours();
|
||||
$this->isTwentyFourHour = $this->localeFormats->isTwentyFourHours();
|
||||
}
|
||||
|
||||
if (true === $this->isTwentyFourHour) {
|
||||
@@ -230,6 +266,6 @@ class DateExtensions extends AbstractExtension
|
||||
*/
|
||||
public function getDurationFormat()
|
||||
{
|
||||
return $this->localeSettings->getDurationFormat();
|
||||
return $this->localeFormats->getDurationFormat();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,14 +10,6 @@
|
||||
namespace App\Twig;
|
||||
|
||||
use App\Constants;
|
||||
use App\Entity\Timesheet;
|
||||
use App\Utils\Duration;
|
||||
use App\Utils\LocaleSettings;
|
||||
use NumberFormatter;
|
||||
use Symfony\Component\Intl\Countries;
|
||||
use Symfony\Component\Intl\Currencies;
|
||||
use Symfony\Component\Intl\Languages;
|
||||
use Symfony\Component\Intl\Locales;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
@@ -27,49 +19,12 @@ use Twig\TwigFunction;
|
||||
*/
|
||||
class Extensions extends AbstractExtension
|
||||
{
|
||||
/**
|
||||
* @var LocaleSettings
|
||||
*/
|
||||
protected $localeSettings;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $locale;
|
||||
/**
|
||||
* @var Duration
|
||||
*/
|
||||
protected $durationFormatter;
|
||||
/**
|
||||
* @var NumberFormatter
|
||||
*/
|
||||
protected $numberFormatter;
|
||||
/**
|
||||
* @var NumberFormatter
|
||||
*/
|
||||
protected $moneyFormatter;
|
||||
|
||||
/**
|
||||
* @param LocaleSettings $localeSettings
|
||||
*/
|
||||
public function __construct(LocaleSettings $localeSettings)
|
||||
{
|
||||
$this->localeSettings = $localeSettings;
|
||||
$this->durationFormatter = new Duration();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new TwigFilter('duration', [$this, 'duration']),
|
||||
new TwigFilter('duration_decimal', [$this, 'durationDecimal']),
|
||||
new TwigFilter('money', [$this, 'money']),
|
||||
new TwigFilter('currency', [$this, 'currency']),
|
||||
new TwigFilter('country', [$this, 'country']),
|
||||
new TwigFilter('language', [$this, 'language']),
|
||||
new TwigFilter('amount', [$this, 'amount']),
|
||||
new TwigFilter('docu_link', [$this, 'documentationLink']),
|
||||
new TwigFilter('multiline_indent', [$this, 'multilineIndent']),
|
||||
];
|
||||
@@ -81,7 +36,6 @@ class Extensions extends AbstractExtension
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('locales', [$this, 'getLocales']),
|
||||
new TwigFunction('class_name', [$this, 'getClassName']),
|
||||
];
|
||||
}
|
||||
@@ -120,115 +74,6 @@ class Extensions extends AbstractExtension
|
||||
return implode(PHP_EOL, $parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms seconds into a duration string.
|
||||
*
|
||||
* @param int|Timesheet $duration
|
||||
* @param bool $decimal
|
||||
* @return string
|
||||
*/
|
||||
public function duration($duration, $decimal = false)
|
||||
{
|
||||
if ($decimal) {
|
||||
return $this->durationDecimal($duration);
|
||||
}
|
||||
|
||||
$duration = $this->getSecondsForDuration($duration);
|
||||
$format = $this->localeSettings->getDurationFormat();
|
||||
|
||||
return $this->formatDuration($duration, $format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms seconds into a decimal formatted duration string.
|
||||
*
|
||||
* @param int|Timesheet $duration
|
||||
* @return string
|
||||
*/
|
||||
public function durationDecimal($duration)
|
||||
{
|
||||
$duration = $this->getSecondsForDuration($duration);
|
||||
|
||||
return $this->getNumberFormatter()->format(number_format($duration / 3600, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|float $amount
|
||||
* @return bool|false|string
|
||||
*/
|
||||
public function amount($amount)
|
||||
{
|
||||
return $this->getNumberFormatter()->format($amount);
|
||||
}
|
||||
|
||||
private function getSecondsForDuration($duration): int
|
||||
{
|
||||
if (null === $duration) {
|
||||
$duration = 0;
|
||||
}
|
||||
|
||||
if ($duration instanceof Timesheet) {
|
||||
if (null === $duration->getEnd()) {
|
||||
$duration = time() - $duration->getBegin()->getTimestamp();
|
||||
} else {
|
||||
$duration = $duration->getDuration();
|
||||
}
|
||||
}
|
||||
|
||||
return (int) $duration;
|
||||
}
|
||||
|
||||
protected function formatDuration(int $seconds, string $format): string
|
||||
{
|
||||
if ($seconds < 0) {
|
||||
return '?';
|
||||
}
|
||||
|
||||
return $this->durationFormatter->format($seconds, $format);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $currency
|
||||
* @return string
|
||||
*/
|
||||
public function currency($currency)
|
||||
{
|
||||
try {
|
||||
return Currencies::getSymbol(strtoupper($currency));
|
||||
} catch (\Exception $ex) {
|
||||
}
|
||||
|
||||
return $currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $language
|
||||
* @return string
|
||||
*/
|
||||
public function language($language)
|
||||
{
|
||||
try {
|
||||
return Languages::getName(strtolower($language), $this->locale);
|
||||
} catch (\Exception $ex) {
|
||||
}
|
||||
|
||||
return $language;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $country
|
||||
* @return string
|
||||
*/
|
||||
public function country($country)
|
||||
{
|
||||
try {
|
||||
return Countries::getName(strtoupper($country));
|
||||
} catch (\Exception $ex) {
|
||||
}
|
||||
|
||||
return $country;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @return string
|
||||
@@ -237,62 +82,4 @@ class Extensions extends AbstractExtension
|
||||
{
|
||||
return Constants::HOMEPAGE . '/documentation/' . $url;
|
||||
}
|
||||
|
||||
private function initLocale()
|
||||
{
|
||||
$locale = $this->localeSettings->getLocale();
|
||||
|
||||
if ($this->locale === $locale) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->locale = $locale;
|
||||
$this->numberFormatter = new NumberFormatter($locale, NumberFormatter::DECIMAL);
|
||||
$this->moneyFormatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
|
||||
}
|
||||
|
||||
private function getNumberFormatter(): NumberFormatter
|
||||
{
|
||||
$this->initLocale();
|
||||
|
||||
return $this->numberFormatter;
|
||||
}
|
||||
|
||||
private function getMoneyFormatter(): NumberFormatter
|
||||
{
|
||||
$this->initLocale();
|
||||
|
||||
return $this->moneyFormatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $amount
|
||||
* @param string $currency
|
||||
* @return string
|
||||
*/
|
||||
public function money($amount, $currency = null)
|
||||
{
|
||||
if (null !== $currency) {
|
||||
return $this->getMoneyFormatter()->formatCurrency($amount, $currency);
|
||||
}
|
||||
|
||||
return $this->getNumberFormatter()->format($amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the list of codes of the locales (languages) enabled in the
|
||||
* application and returns an array with the name of each locale written
|
||||
* in its own language (e.g. English, Français, Español, etc.)
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getLocales()
|
||||
{
|
||||
$locales = [];
|
||||
foreach ($this->localeSettings->getAvailableLanguages() as $locale) {
|
||||
$locales[] = ['code' => $locale, 'name' => Locales::getName($locale, $locale)];
|
||||
}
|
||||
|
||||
return $locales;
|
||||
}
|
||||
}
|
||||
|
||||
219
src/Twig/LocaleExtensions.php
Normal file
219
src/Twig/LocaleExtensions.php
Normal file
@@ -0,0 +1,219 @@
|
||||
<?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\Configuration\LanguageFormattings;
|
||||
use App\Constants;
|
||||
use App\Entity\Timesheet;
|
||||
use App\Utils\Duration;
|
||||
use App\Utils\LocaleFormats;
|
||||
use App\Utils\LocaleHelper;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Intl\Languages;
|
||||
use Symfony\Component\Intl\Locales;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
/**
|
||||
* Locale specific Twig extensions
|
||||
*/
|
||||
final class LocaleExtensions extends AbstractExtension
|
||||
{
|
||||
/**
|
||||
* @var LocaleFormats
|
||||
*/
|
||||
private $localeFormats;
|
||||
/**
|
||||
* @var Duration
|
||||
*/
|
||||
private $durationFormatter;
|
||||
/**
|
||||
* @var LocaleHelper
|
||||
*/
|
||||
private $helper;
|
||||
/**
|
||||
* @var LanguageFormattings
|
||||
*/
|
||||
private $formats;
|
||||
|
||||
public function __construct(RequestStack $requestStack, LanguageFormattings $formats)
|
||||
{
|
||||
$locale = Constants::DEFAULT_LOCALE;
|
||||
|
||||
// request is null in a console command
|
||||
if (null !== $requestStack->getMasterRequest()) {
|
||||
$locale = $requestStack->getMasterRequest()->getLocale();
|
||||
}
|
||||
|
||||
$this->durationFormatter = new Duration();
|
||||
$this->formats = $formats;
|
||||
$this->setLocale($locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new TwigFilter('duration', [$this, 'duration']),
|
||||
new TwigFilter('duration_decimal', [$this, 'durationDecimal']),
|
||||
new TwigFilter('money', [$this, 'money']),
|
||||
new TwigFilter('currency', [$this, 'currency']),
|
||||
new TwigFilter('country', [$this, 'country']),
|
||||
new TwigFilter('language', [$this, 'language']),
|
||||
new TwigFilter('amount', [$this, 'amount']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('locales', [$this, 'getLocales']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows to switch the locale used for all twig filter and functions.
|
||||
*
|
||||
* @param string $locale
|
||||
*/
|
||||
public function setLocale(string $locale)
|
||||
{
|
||||
$this->helper = new LocaleHelper($locale);
|
||||
$this->localeFormats = new LocaleFormats($this->formats, $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms seconds into a duration string.
|
||||
*
|
||||
* @param int|Timesheet $duration
|
||||
* @param bool $decimal
|
||||
* @return string
|
||||
*/
|
||||
public function duration($duration, $decimal = false)
|
||||
{
|
||||
if ($decimal) {
|
||||
return $this->durationDecimal($duration);
|
||||
}
|
||||
|
||||
$seconds = $this->getSecondsForDuration($duration);
|
||||
$format = $this->localeFormats->getDurationFormat();
|
||||
|
||||
return $this->formatDuration($seconds, $format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms seconds into a decimal formatted duration string.
|
||||
*
|
||||
* @param int|Timesheet $duration
|
||||
* @return string
|
||||
*/
|
||||
public function durationDecimal($duration)
|
||||
{
|
||||
$seconds = $this->getSecondsForDuration($duration);
|
||||
|
||||
return $this->helper->durationDecimal($seconds);
|
||||
}
|
||||
|
||||
private function getSecondsForDuration($duration): int
|
||||
{
|
||||
if (null === $duration) {
|
||||
$duration = 0;
|
||||
}
|
||||
|
||||
if ($duration instanceof Timesheet) {
|
||||
if (null === $duration->getEnd()) {
|
||||
$duration = time() - $duration->getBegin()->getTimestamp();
|
||||
} else {
|
||||
$duration = $duration->getDuration();
|
||||
}
|
||||
}
|
||||
|
||||
return (int) $duration;
|
||||
}
|
||||
|
||||
private function formatDuration(int $seconds, string $format): string
|
||||
{
|
||||
if ($seconds < 0) {
|
||||
return '?';
|
||||
}
|
||||
|
||||
return $this->durationFormatter->format($seconds, $format);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|float $amount
|
||||
* @return bool|false|string
|
||||
*/
|
||||
public function amount($amount)
|
||||
{
|
||||
return $this->helper->amount($amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $currency
|
||||
* @return string
|
||||
*/
|
||||
public function currency($currency)
|
||||
{
|
||||
return $this->helper->currency($currency);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $language
|
||||
* @return string
|
||||
*/
|
||||
public function language($language)
|
||||
{
|
||||
return $this->helper->language($language);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $country
|
||||
* @return string
|
||||
*/
|
||||
public function country($country)
|
||||
{
|
||||
return $this->helper->country($country);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $amount
|
||||
* @param string|null $currency
|
||||
* @param bool $withCurrency
|
||||
* @return string
|
||||
*/
|
||||
public function money($amount, ?string $currency = null, bool $withCurrency = true)
|
||||
{
|
||||
return $this->helper->money($amount, $currency, $withCurrency);
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the list of codes of the locales (languages) enabled in the
|
||||
* application and returns an array with the name of each locale written
|
||||
* in its own language (e.g. English, Français, Español, etc.)
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getLocales()
|
||||
{
|
||||
$locales = [];
|
||||
foreach ($this->localeFormats->getAvailableLanguages() as $locale) {
|
||||
$locales[] = ['code' => $locale, 'name' => Locales::getName($locale, $locale)];
|
||||
}
|
||||
|
||||
return $locales;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user