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

Added new filters localizednumber and localizedcurrency on Intl Extension #55

Closed
wants to merge 2 commits into from
Closed

Conversation

Garfield-fr
Copy link
Contributor

Example:

Number: {{ number | localizednumber }}
Currency: {{ number | localizedcurrency }} ou {{ number | localizedcurrency('CHF', 'fr_CH') }}

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

function getNumberFormatter($locale, $style)
Copy link
Member

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I change this.

@gido
Copy link

gido commented Jul 17, 2012

Any news ?
How can I help on this ?

@blaugueux
Copy link
Contributor

It will be great to merge this PR.

@fabpot ping

@mvrhov
Copy link

mvrhov commented Dec 18, 2012

@fabpot any chances of merging this?

@Garfield-fr
Copy link
Contributor Author

@fabpot any news for this PR ? Thanks

@stof
Copy link
Member

stof commented Jan 4, 2013

@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(),
Copy link
Member

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

@pierre-b
Copy link

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);
  }

@blaugueux
Copy link
Contributor

@pierre-b Why a boolean instead of an integer ?

@pierre-b
Copy link

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.

@lunetics
Copy link

Why does that useful thing not get merged?

@lenybernard
Copy link

Any news about a merge date @fabpot ?

@Trainmaster
Copy link
Contributor

I'd like to address two things:

  • Naming: What about intl_date, intl_currency? Shorter and better readability.
  • Performance: Currently on each call a formatter is constructed. Wouldn't it make sense to cache them somehow? Maybe in a static variable.

@fabpot
Copy link
Contributor

fabpot commented Aug 5, 2014

see #115

@fabpot fabpot closed this Aug 5, 2014
fabpot added a commit that referenced this pull request Aug 5, 2014
…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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.