language specific money and date display (#180)

* improved date translation
* language specific date and money display
This commit is contained in:
Kevin Papst
2018-06-25 16:41:12 +02:00
committed by Simon Schaufelberger
parent f563e0703d
commit d4a46cf257
14 changed files with 308 additions and 37 deletions

View File

@@ -11,6 +11,8 @@ namespace App\Twig;
use App\Entity\Timesheet;
use App\Utils\Duration;
use NumberFormatter;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Intl\Intl;
use Twig\TwigFilter;
@@ -22,19 +24,36 @@ class Extensions extends \Twig_Extension
/**
* @var string[]
*/
private $locales;
protected $locales;
/**
* @var string
*/
protected $locale;
/**
* @var Duration
*/
protected $durationFormatter;
/**
* @var NumberFormatter
*/
protected $numberFormatter;
/**
* @var RequestStack
*/
protected $requestStack;
/**
* Extensions constructor.
* @param string $locales
* @param string $locale
*/
public function __construct($locales)
public function __construct(RequestStack $requestStack, $locales)
{
$this->requestStack = $requestStack;
$this->locales = explode('|', $locales);
$this->durationFormatter = new Duration();
}
@@ -107,14 +126,32 @@ class Extensions extends \Twig_Extension
*/
public function money($amount, $currency = null)
{
$result = number_format(round($amount, 2), 2);
$locale = $this->getLocale();
if ($this->locale !== $locale) {
$this->locale = $locale;
$this->numberFormatter = new NumberFormatter($locale, NumberFormatter::DECIMAL);
}
$fractionDigits = Intl::getCurrencyBundle()->getFractionDigits($currency);
$amount = round($amount, $fractionDigits);
$result = $this->numberFormatter->format($amount);
if (null !== $currency) {
$result .= ' ' . Intl::getCurrencyBundle()->getCurrencySymbol($currency);
$result .= ' ' . Intl::getCurrencyBundle()->getCurrencySymbol($currency, $locale);
}
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