Description: This ViewHelper uses the given locale in the site configuration
- TYPO3 >= v9
- Configured site management
- PHP module
intl
- If set, it removes the decimals when the value is a number without decimal values
Description: This ViewHelper uses the given locale in the site configuration
intl
<?php | |
namespace Vendor\ExtensionName\ViewHelpers\Format; | |
use TYPO3\CMS\Core\Context\Context; | |
use TYPO3\CMS\Core\Site\SiteFinder; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; | |
class NumberViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Format\NumberViewHelper | |
{ | |
public function initializeArguments() | |
{ | |
parent::initializeArguments(); | |
$this->registerArgument('removeDecimalsIfIntegerValue', 'bool', 'Remove decimals if number is an integer value', false, false); | |
} | |
/** | |
* Format the numeric value as a number with grouped thousands, decimal point and | |
* precision. | |
* | |
* @param array $arguments | |
* @param \Closure $renderChildrenClosure | |
* @param RenderingContextInterface $renderingContext | |
* | |
* @return string The formatted number | |
*/ | |
public static function renderStatic( | |
array $arguments, | |
\Closure $renderChildrenClosure, | |
RenderingContextInterface $renderingContext | |
) { | |
$decimals = $arguments['decimals']; | |
$decimalSeparator = $arguments['decimalSeparator']; | |
$thousandsSeparator = $arguments['thousandsSeparator']; | |
$stringToFormat = $renderChildrenClosure(); | |
$site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByIdentifier('identifier'); | |
if ($site) { | |
$languageId = GeneralUtility::makeInstance(Context::class)->getAspect('language')->getId(); | |
$numberFormatter = new \NumberFormatter( | |
$site->getLanguageById($languageId)->getLocale(), | |
\NumberFormatter::DECIMAL | |
); | |
$decimalSeparator = $numberFormatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); | |
$thousandsSeparator = $numberFormatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL); | |
} | |
if ($arguments['removeDecimalsIfIntegerValue'] && self::isNotFloatNumber($stringToFormat)) { | |
$decimals = 0; | |
} | |
return number_format((float)$stringToFormat, $decimals, $decimalSeparator, $thousandsSeparator); | |
} | |
private static function isNotFloatNumber($number): bool | |
{ | |
return (float)$number == (int)$number; | |
} | |
} |
Thanks for this hint. Changed it to use
NumberFormatter
.