Closed damienalexandre closed 11 months ago
Well, the PHP documentation itself says that \NumberFormatter::PADDING_POSITION
is a number attribute and not a style: https://www.php.net/manual/fr/class.numberformatter.php#numberformatter.constants.padding-position
So I guess the Twig implementation is wrong because it was done based on the PHP documentation. I suggest you to report an issue on PHP to fix things.
My goal is to format this price: 673000111.12 like this: 673 M € (known as short-form compact notation in ICU).
I came accros this issue when trying to replicate this code in Twig:
As you can see it looks like PHP has support for compact notation in both
formatCurrency
andformat
🎉 (even if the "style" is not set to "currency", we can callformatCurrency
, that's confusing).With Twig IntlExtension
format_number()
method, you can only set one of those styles:https://github.com/twigphp/Twig/blob/df75bfef370907f3842c321280b64c42f8507ecb/extra/intl-extra/IntlExtension.php#L72-L80
No trace of the one I used in PHP:
\NumberFormatter::PADDING_POSITION
. The IntlExtension treat that style as a number attribute:https://github.com/twigphp/Twig/blob/df75bfef370907f3842c321280b64c42f8507ecb/extra/intl-extra/IntlExtension.php#L95
But when used as an attribute, it does nothing ❌
And forcing that style is not possible:
What would be needed to fix this, is some kind of trick where, if a padding_position attribute is given, we set the style to
\NumberFormatter::PADDING_POSITION
and call eitherformatCurrency
orformat
as usual on that "padding" formatter.But I'm a bit torn appart because nothing in the documentation tells that
\NumberFormatter::PADDING_POSITION
is a "style" and it make no sense... It could even be a bug in PHP and it's working by accident.Other implementations
In the PHP World I did not found a lot of usages of that "style".
https://github.com/akaunting/laravel-money/blob/37a56d8320a0a9ffa4dcecb218d2c55ba189ce06/src/Money.php#L586-L611
https://github.com/StarCitizenTools/mediawiki-skins-Citizen/blob/c793959416ce28fff0ddebebff6252f005b6aa52/includes/Partials/Drawer.php#L143-L149
In JavaScript it's known as the compact notation (much clearer name!):
I'm very curious about getting some feedback on that matter, I feel we can do a patch in Twig IntlExtension to get this nice feature, but also that there is something wrong in PHP itself about this as it supposed to be an attribute:
https://github.com/php/php-src/blob/30c5ae421940ed9597d838f3bcff6ea3c8a34047/ext/intl/formatter/formatter_attr.c#L55