-
-
Notifications
You must be signed in to change notification settings - Fork 274
Added new filters localizednumber and localizedcurrency on Intl Extension #55
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ class Twig_Extensions_Extension_Intl extends Twig_Extension | |
{ | ||
public function __construct() | ||
{ | ||
if (!class_exists('IntlDateFormatter')) { | ||
if (!class_exists('IntlDateFormatter') || !class_exists('NumberFormatter')) { | ||
throw new RuntimeException('The intl extension is needed to use intl-based filters.'); | ||
} | ||
} | ||
|
@@ -26,7 +26,9 @@ public function __construct() | |
public function getFilters() | ||
{ | ||
return array( | ||
'localizeddate' => new Twig_Filter_Function('twig_localized_date_filter'), | ||
'localizeddate' => new Twig_Filter_Function('twig_localized_date_filter'), | ||
'localizednumber' => new Twig_Filter_Function('twig_localized_number_filter'), | ||
'localizedcurrency' => new Twig_Filter_Function('twig_localized_currency_filter'), | ||
); | ||
} | ||
|
||
|
@@ -68,3 +70,51 @@ function twig_localized_date_filter($date, $dateFormat = 'medium', $timeFormat = | |
|
||
return $formatter->format($date->getTimestamp()); | ||
} | ||
|
||
function twig_localized_number_filter($number, $style = 'decimal', $format = 'default', $currency = null, $locale = null ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused argument There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and extra space before the closing brace |
||
{ | ||
$formatter = getNumberFormatter( | ||
$locale !== null ? $locale : Locale::getDefault(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can simplify it as |
||
$style | ||
); | ||
|
||
$formatValues = array( | ||
'default' => NumberFormatter::TYPE_DEFAULT, | ||
'int32' => NumberFormatter::TYPE_INT32, | ||
'int64' => NumberFormatter::TYPE_INT64, | ||
'double' => NumberFormatter::TYPE_DOUBLE, | ||
'currency' => NumberFormatter::TYPE_CURRENCY, | ||
); | ||
|
||
return $formatter->format( | ||
$number, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should keep it on 1 line |
||
$formatValues[$format]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should add some error handling for invalid formats |
||
} | ||
|
||
function twig_localized_currency_filter($number, $currency = null, $locale = null) | ||
{ | ||
$formatter = getNumberFormatter( | ||
$locale !== null ? $locale : Locale::getDefault(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
'currency' | ||
); | ||
|
||
return $formatter->formatCurrency($number, $currency); | ||
} | ||
|
||
function getNumberFormatter($locale, $style) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all functions defined by twig are prefixed by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. I change this. |
||
{ | ||
$styleValues = array( | ||
'decimal' => NumberFormatter::DECIMAL, | ||
'currency' => NumberFormatter::CURRENCY, | ||
'percent' => NumberFormatter::PERCENT, | ||
'scientific' => NumberFormatter::SCIENTIFIC, | ||
'spellout' => NumberFormatter::SPELLOUT, | ||
'ordinal' => NumberFormatter::ORDINAL, | ||
'duration' => NumberFormatter::DURATION, | ||
); | ||
|
||
return NumberFormatter::create( | ||
$locale !== null ? $locale : Locale::getDefault(), | ||
$styleValues[$style] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should handle invalid styles |
||
); | ||
} |
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.
@fabpot should we rename them to
localized_currency
and so on ? It would be more readableThere 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.
Can't we rename them with the same name we had in symfony 1.x:
format_currency
,format_date
...?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.
Why would you like to rename this ? I use the same syntax of the first filter "localizeddate".
In Twig, we are "number_format" on Twig Core Extension. It's not a good idea.
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.
@hhamon keeping
localized
in the name is a good idea IMO. Twig already has filters to format date and numbers in the core. The difference here is that these filters are using intlThere 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.
@stof
localizeddate
is allready in this extension. It's not my add.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.
and I know it is the same syntax than the previous filter (added by myself following a discussion in the Twig issue tracker). But I find these names quite unreadable, which is why I suggest renaming them
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.
Maybe we can go for something like
l10n_date
?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.
I agree with @hhamon l10n prefix is shorter
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.
Why not simply intl_date, intl_number, intl_currency? The advantages are obivous: Shorter and in reference to the intl Extension.