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

Commit

Permalink
Merge branch 'hotfix/73'
Browse files Browse the repository at this point in the history
Close #73
Fixes #72
  • Loading branch information
weierophinney committed May 17, 2017
2 parents 56d907c + eb3b3fc commit 4a06865
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ All notable changes to this project will be documented in this file, in reverse
- [#62](https://github.com/zendframework/zend-i18n/pull/62) fixes an issue with
how the gettext adapter resolves `PoEdit` source keywords when a text_domain is
defined.
- [#73](https://github.com/zendframework/zend-i18n/pull/73) provides a
workaround within the `CurrencyFormat` view helper for an ICU bug
(http://bugs.icu-project.org/trac/ticket/10997).

## 2.7.3 - 2016-06-07

Expand Down
31 changes: 30 additions & 1 deletion src/View/Helper/CurrencyFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ class CurrencyFormat extends AbstractHelper
*/
protected $showDecimals = true;

/**
* Special condition to be checked due to ICU bug:
* http://bugs.icu-project.org/trac/ticket/10997
*
* @var bool
*/
protected $correctionNeeded = false;



/**
* @throws Exception\ExtensionNotLoadedException if ext/intl is not present
*/
Expand Down Expand Up @@ -132,11 +142,16 @@ protected function formatCurrency(

if ($showDecimals) {
$this->formatters[$formatterId]->setAttribute(NumberFormatter::FRACTION_DIGITS, 2);
$this->correctionNeeded = false;
} else {
$this->formatters[$formatterId]->setAttribute(NumberFormatter::FRACTION_DIGITS, 0);
$defaultCurrencyCode = $this->formatters[$formatterId]->getTextAttribute(NumberFormatter::CURRENCY_CODE);
$this->correctionNeeded = $defaultCurrencyCode !== $currencyCode;
}

return $this->formatters[$formatterId]->formatCurrency($number, $currencyCode);
$formattedNumber = $this->formatters[$formatterId]->formatCurrency($number, $currencyCode);

return $this->correctionNeeded ? $this->correctICUBug($formattedNumber, $this->formatters[$formatterId]) : $formattedNumber;
}

/**
Expand Down Expand Up @@ -230,4 +245,18 @@ public function shouldShowDecimals()
{
return $this->showDecimals;
}



/**
* @param string $formattedNumber
* @param NumberFormatter $formatter
* @return string
*/
private function correctICUBug($formattedNumber, NumberFormatter $formatter)
{
$pattern = sprintf('/\%s\d+$/', $formatter->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL));

return preg_replace($pattern, '', $formattedNumber);
}
}
2 changes: 2 additions & 0 deletions test/View/Helper/CurrencyFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public function currencyProvider()
//array('en_US', 'EUR', false, 1234567.891234567890000, null, '€1,234,568'),
//array('en_US', 'EUR', false, 1234567.891234567890000, null, '€1,234,568'),
['en_US', 'USD', false, 1234567.891234567890000, null, '$1,234,568'],
/* @see http://bugs.icu-project.org/trac/ticket/10997 */
['en_US', 'EUR', false, 1234567.891234567890000, null, '€1,234,567'],
];
}

Expand Down

0 comments on commit 4a06865

Please sign in to comment.