Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
feature #115 Add "localizednumber" and "localizedcurrency" filters (G…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
fabpot committed Aug 5, 2014
2 parents 8699f80 + 76f532d commit 6af92b6
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 10 deletions.
95 changes: 85 additions & 10 deletions doc/intl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,88 @@ Arguments
* ``format``: Optional pattern to use when formatting or parsing. Possible
patterns are documented in the `ICU user guide`_.

.. _`strtotime`: http://php.net/strtotime
.. _`DateTime`: http://php.net/DateTime
.. _`Unix timestamps`: http://en.wikipedia.org/wiki/Unix_time
.. _`IntlDateFormatter::create()`: http://php.net/manual/en/intldateformatter.create.php
.. _`IntlDateFormatter::NONE`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.none
.. _`IntlDateFormatter::SHORT`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.short
.. _`IntlDateFormatter::MEDIUM`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.medium
.. _`IntlDateFormatter::LONG`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.long
.. _`IntlDateFormatter::FULL`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.full
.. _`ICU user guide`: http://userguide.icu-project.org/formatparse/datetime
``localizednumber``
-------------------

Use the ``localizednumber`` filter to format numbers into a localized string
representating the number.

.. code-block:: jinja
{{ product.quantity|localizednumber }}
.. note::

Internally, Twig uses the PHP `NumberFormatter::create()`_ function for
the number.

Arguments
~~~~~~~~~

* ``style``: Optional date format (default: 'decimal'). Choose one of these formats:

* 'decimal': `NumberFormatter::DECIMAL`_
* 'currency': `NumberFormatter::CURRENCY`_
* 'percent': `NumberFormatter::PERCENT`_
* 'scientific': `NumberFormatter::SCIENTIFIC`_
* 'spellout': `NumberFormatter::SPELLOUT`_
* 'ordinal': `NumberFormatter::ORDINAL`_
* 'duration': `NumberFormatter::DURATION`_

* ``type``: Optional formatting type to use (default: 'default'). Choose one of these types:

* 'default': `NumberFormatter::TYPE_DEFAULT`_
* 'int32': `NumberFormatter::TYPE_INT32`_
* 'int64': `NumberFormatter::TYPE_INT64`_
* 'double': `NumberFormatter::TYPE_DOUBLE`_
* 'currency': `NumberFormatter::TYPE_CURRENCY`_

* ``locale``: The locale used for the format. If ``NULL`` is given, Twig will
use ``Locale::getDefault()``

``localizedcurrency``
---------------------

Use the ``localizedcurrency`` filter to format a currency value into a localized string.

.. code-block:: jinja
{{ product.price|localizedcurrency('EUR') }}
.. note::

Internally, Twig uses the PHP `NumberFormatter::create()`_ function for
the number.

Arguments
~~~~~~~~~

* ``currency``: The 3-letter ISO 4217 currency code indicating the currency to use.

* ``locale``: The locale used for the format. If ``NULL`` is given, Twig will
use ``Locale::getDefault()``


.. _`strtotime`: http://php.net/strtotime
.. _`DateTime`: http://php.net/DateTime
.. _`Unix timestamps`: http://en.wikipedia.org/wiki/Unix_time
.. _`IntlDateFormatter::create()`: http://php.net/manual/en/intldateformatter.create.php
.. _`IntlDateFormatter::NONE`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.none
.. _`IntlDateFormatter::SHORT`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.short
.. _`IntlDateFormatter::MEDIUM`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.medium
.. _`IntlDateFormatter::LONG`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.long
.. _`IntlDateFormatter::FULL`: http://php.net/manual/en/class.intldateformatter.php#intldateformatter.constants.full
.. _`ICU user guide`: http://userguide.icu-project.org/formatparse/datetime
.. _`NumberFormatter::create()`: http://php.net/manual/en/numberformatter.create.php
.. _`NumberFormatter::DECIMAL`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.decimal
.. _`NumberFormatter::CURRENCY`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.currency
.. _`NumberFormatter::PERCENT`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.percent
.. _`NumberFormatter::SCIENTIFIC`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.scientific
.. _`NumberFormatter::SPELLOUT`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.spellout
.. _`NumberFormatter::ORDINAL`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.ordinal
.. _`NumberFormatter::DURATION`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.duration
.. _`NumberFormatter::TYPE_DEFAULT`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.type-default
.. _`NumberFormatter::TYPE_INT32`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.type-int32
.. _`NumberFormatter::TYPE_INT64`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.type-int64
.. _`NumberFormatter::TYPE_DOUBLE`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.type-double
.. _`NumberFormatter::TYPE_CURRENCY`: http://php.net/manual/en/class.numberformatter.php#numberformatter.constants.type-currency
69 changes: 69 additions & 0 deletions lib/Twig/Extensions/Extension/Intl.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function getFilters()
{
return array(
new Twig_SimpleFilter('localizeddate', 'twig_localized_date_filter', array('needs_environment' => true)),
new Twig_SimpleFilter('localizednumber', 'twig_localized_number_filter'),
new Twig_SimpleFilter('localizedcurrency', 'twig_localized_currency_filter'),
);
}

Expand Down Expand Up @@ -64,3 +66,70 @@ function twig_localized_date_filter(Twig_Environment $env, $date, $dateFormat =

return $formatter->format($date->getTimestamp());
}

function twig_localized_number_filter($number, $style = 'decimal', $type = 'default', $locale = null)
{
static $typeValues = array(
'default' => NumberFormatter::TYPE_DEFAULT,
'int32' => NumberFormatter::TYPE_INT32,
'int64' => NumberFormatter::TYPE_INT64,
'double' => NumberFormatter::TYPE_DOUBLE,
'currency' => NumberFormatter::TYPE_CURRENCY,
);

$formatter = twig_get_number_formatter($locale, $style);

if (!isset($typeValues[$type])) {
throw new Twig_Error_Syntax(sprintf('The type "%s" does not exist. Known types are: "%s"', $type, implode('", "', array_keys($typeValues))));
}

return $formatter->format($number, $typeValues[$type]);
}

function twig_localized_currency_filter($number, $currency = null, $locale = null)
{
$formatter = twig_get_number_formatter($locale, 'currency');

return $formatter->formatCurrency($number, $currency);
}

/**
* Gets a number formatter instance according to given locale and formatter
*
* @param string $locale Locale in which the number would be formatted
* @param int $style Style of the formatting
*
* @return NumberFormatter A NumberFormatter instance
*/
function twig_get_number_formatter($locale, $style)
{
static $formatter, $currentStyle;

$locale = $locale !== null ? $locale : Locale::getDefault();

if ($formatter && $formatter->getLocale() === $locale && $currentStyle === $style) {
// Return same instance of NumberFormatter if parameters are the same
// to those in previous call
return $formatter;
}

static $styleValues = array(
'decimal' => NumberFormatter::DECIMAL,
'currency' => NumberFormatter::CURRENCY,
'percent' => NumberFormatter::PERCENT,
'scientific' => NumberFormatter::SCIENTIFIC,
'spellout' => NumberFormatter::SPELLOUT,
'ordinal' => NumberFormatter::ORDINAL,
'duration' => NumberFormatter::DURATION,
);

if (!isset($styleValues[$style])) {
throw new Twig_Error_Syntax(sprintf('The style "%s" does not exist. Known styles are: "%s"', $style, implode('", "', array_keys($styleValues))));
}

$currentStyle = $style;

$formatter = NumberFormatter::create($locale, $styleValues[$style]);

return $formatter;
}

0 comments on commit 6af92b6

Please sign in to comment.