-
-
Notifications
You must be signed in to change notification settings - Fork 274
Added new filters localizednumber and localizedcurrency on Intl Extension #55
Conversation
return $formatter->formatCurrency($number, $currency); | ||
} | ||
|
||
function getNumberFormatter($locale, $style) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all functions defined by twig are prefixed by twig_
to avoid conflicts. Please do it here too (and _twig_
may be better here as it is only an internal function, to be consistent with Twig itself)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I change this.
Any news ? |
It will be great to merge this PR. @fabpot ping |
@fabpot any chances of merging this? |
@fabpot any news for this PR ? Thanks |
@fabpot ping |
function twig_localized_number_filter($number, $style = 'decimal', $format = 'default', $currency = null, $locale = null ) | ||
{ | ||
$formatter = twig_get_number_formatter( | ||
$locale !== null ? $locale : Locale::getDefault(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can simplify it as twig_get_number_formatter($locale, $style)
as twig_get_number_formatter
already handles the default
You should also add possibility to remove decimals for displaying currencies: $99.00 => $99 public function twig_localized_currency_filter($number, $currency = null, $locale = null, $decimals = true)
{
$formatter = $this->twig_get_number_formatter(
$locale !== null ? $locale : Locale::getDefault(),
'currency'
);
$formatter->setTextAttribute(NumberFormatter::CURRENCY_CODE, $currency);
if(!$decimals) $formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 0);
return $formatter->format($number);
} |
@pierre-b Why a |
usage looks like this for displaying $99 instead of $99.00: {{ price|localizedcurrency(currency, locale, false) }} It could also be useful to use an integer to choose numbers of decimals (ex: $99.000)... but it's not common though. |
Why does that useful thing not get merged? |
Any news about a merge date @fabpot ? |
I'd like to address two things:
|
see #115 |
…arfield-fr, michaelperrin) This PR was merged into the 1.1.x-dev branch. Discussion ---------- Add "localizednumber" and "localizedcurrency" filters Hello, This is a new version of PR #55 proposed by @Garfield-fr: * Rebased on the `master` branch * Made some changes according to comments made by @stof: remove unneeded arguments, checks on provided values * Added documentation As for the name of these filters, I would go for `localized_date`, `localized_number` and `localized_currency` but as `localizeddate` already exists, I preferred to keep the (weird) naming convention to keep backward compatibility. Naming conventions changes could be made in an other PR, for a future version of Twig. I didn't add tests as there is currently no test for any filter (apart from grammar checks). I can squash all commits into one if needed. Commits ------- 76f532d Add documentation for localizednumber and localizedcurrency filters f6c4130 Add checks on "localizednumber" and "localizedcurrency" filters and small tweaks 380f7f0 Rename getNumberFormatter to twig_get_number_formatter 448a309 Added new filters localizednumber and localizedcurrency on Intl extension
Example:
Number: {{ number | localizednumber }}
Currency: {{ number | localizedcurrency }} ou {{ number | localizedcurrency('CHF', 'fr_CH') }}