Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added Number extension #27

Open
wants to merge 2 commits into from

6 participants

@merk

Added a Intl NumberFormatter filter that falls back to money_format if it is not available.

lib/Twig/Extensions/Extension/Number.php
((33 lines not shown))
+ */
+ public function getName()
+ {
+ return 'Number';
+ }
+}
+
+if (class_exists('NumberFormatter')) {
+ function twig_format_currency($value, $currency)
+ {
+ static $formatter;
+ if (null === $formatter) {
+ $formatter = new NumberFormatter(Locale::getDefault(), NumberFormatter::CURRENCY);
+ }
+
+ return $formatter->formatCurrency($value, $currency);
@stof
stof added a note

What about making the locale configurable with a third parameter (and using the default when it is not given) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@merk

I've added support to override the locale, but it seems like a bit of a hack to call setlocale() for the non Intl function. Not sure if there is a better way?

@stof stof commented on the diff
lib/Twig/Extensions/Extension/Number.php
((35 lines not shown))
+ {
+ return 'Number';
+ }
+}
+
+if (class_exists('NumberFormatter')) {
+ function twig_format_currency($value, $currency, $locale = null)
+ {
+ static $formatter;
+
+ if (null === $locale) {
+ $locale = Locale::getDefault();
+ }
+
+ if (null === $formatter) {
+ $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
@stof
stof added a note

you should not use a static formatter as it will fail when using the method several times with different locales.

@sstok
sstok added a note

Actually you can use a static but compare the locale of the object with the given one (that is how I do it).
Most users will not change the locale during runtime so keeping this 'cache' with a locale check is better imo.

(null === $formatter || $locale !== $formatter->getLocale())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof

You should also add some tests

@merk
@Seldaek

Could you also take in the changes from #14 (cc @falmp) and consolidate it all in one extension?

@stof

@merk ping ?

also, the implementation using intl could probably be added in the Intl extension I submitted in #30 instead

@merk

@stof: In hopsital, wont get a chance to do anything. SonataIntlBundle provides everything I need so I probably wont be dedicating time to this especially since I dont need 5.2 support.

@jnonon

Any update in this feature?

@merk

If you dont need 5.2, https://github.com/sonata-project/SonataIntlBundle has NumberFormatter support.

@stof stof commented on the diff
lib/Twig/Extensions/Extension/Number.php
((26 lines not shown))
+ );
+ }
+
+ /**
+ * Name of this extension
+ *
+ * @return string
+ */
+ public function getName()
+ {
+ return 'Number';
+ }
+}
+
+if (class_exists('NumberFormatter')) {
+ function twig_format_currency($value, $currency, $locale = null)
@stof
stof added a note

This part should be removed in favor of #55 IMO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jrobeson

a localizedcurrency and localizednumber filter has been merged. what should happen here now?

@stof

@jrobeson this PR should be closed IMO, as localizedcurrency is the same than what is done here when Intl is available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 10, 2011
  1. @merk

    Added Number extension

    merk authored
  2. @merk
This page is out of date. Refresh to see the latest.
Showing with 68 additions and 0 deletions.
  1. +68 −0 lib/Twig/Extensions/Extension/Number.php
View
68 lib/Twig/Extensions/Extension/Number.php
@@ -0,0 +1,68 @@
+<?php
+
+/**
+ * This file is part of Twig.
+ *
+ * (c) 2009 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @author Tim Nagel <tim@nagel.com.au>
+ * @package Twig
+ * @subpackage Twig-extensions
+ */
+class Twig_Extensions_Extension_Number extends Twig_Extension
+{
+ /**
+ * Returns a list of filters.
+ *
+ * @return array
+ */
+ public function getFilters()
+ {
+ return array(
+ 'currency' => new Twig_Filter_Function('twig_format_currency'),
+ );
+ }
+
+ /**
+ * Name of this extension
+ *
+ * @return string
+ */
+ public function getName()
+ {
+ return 'Number';
+ }
+}
+
+if (class_exists('NumberFormatter')) {
+ function twig_format_currency($value, $currency, $locale = null)
@stof
stof added a note

This part should be removed in favor of #55 IMO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ static $formatter;
+
+ if (null === $locale) {
+ $locale = Locale::getDefault();
+ }
+
+ if (null === $formatter) {
+ $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
@stof
stof added a note

you should not use a static formatter as it will fail when using the method several times with different locales.

@sstok
sstok added a note

Actually you can use a static but compare the locale of the object with the given one (that is how I do it).
Most users will not change the locale during runtime so keeping this 'cache' with a locale check is better imo.

(null === $formatter || $locale !== $formatter->getLocale())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+
+ return $formatter->formatCurrency($value, $currency);
+ }
+} else {
+ function twig_format_currency($value, $currency = null, $locale = null)
+ {
+ if (null !== $currency) {
+ throw new \LogicException("You must have Intl enabled to specify a currency. Pass null if you\'re trying to set the locale.");
+ }
+
+ if (null !== $locale) {
+ setlocale(LC_MONETARY, $locale);
+ }
+
+ return money_format("%i", $value);
+ }
+}
Something went wrong with that request. Please try again.