diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index df5686f7dc..dfba9da5ee 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -67,6 +67,7 @@ Yii Framework 2 Change Log - Enh #3574: Add integrity check support for SQLite (zeeke) - Enh #3597: Nested array support for HTML5 custom "data-*" attributes (armab) - Enh #3607: Added support for limit in migrations actions: history, new, redo (Ragazzo) +- Enh #3631: Added property `currencyCode` to `yii\i18n\Formatter` (leandrogehlen) - Enh #3636: Hide menu container tag with empty items in `yii\widgets\Menu` (arturf) - Enh #3643: Improved Mime-Type detection by using the `mime.types` file from apache http project to dected mime types by file extension (cebe, pavel-voronin, trejder) - Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue) diff --git a/framework/i18n/Formatter.php b/framework/i18n/Formatter.php index 7ada01c43a..98f5287875 100644 --- a/framework/i18n/Formatter.php +++ b/framework/i18n/Formatter.php @@ -73,6 +73,11 @@ class Formatter extends \yii\base\Formatter * If not set, the thousand separator corresponding to [[locale]] will be used. */ public $thousandSeparator; + /** + * @var string the international currency code displayed when formatting a number. + * If not set, the currency code corresponding to [[locale]] will be used. + */ + public $currencyCode; /** * Initializes the component. @@ -88,7 +93,7 @@ class Formatter extends \yii\base\Formatter if ($this->locale === null) { $this->locale = Yii::$app->language; } - if ($this->decimalSeparator === null || $this->thousandSeparator === null) { + if ($this->decimalSeparator === null || $this->thousandSeparator === null || $this->currencyCode === null) { $formatter = new NumberFormatter($this->locale, NumberFormatter::DECIMAL); if ($this->decimalSeparator === null) { $this->decimalSeparator = $formatter->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); @@ -96,6 +101,9 @@ class Formatter extends \yii\base\Formatter if ($this->thousandSeparator === null) { $this->thousandSeparator = $formatter->getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL); } + if ($this->currencyCode === null) { + $this->currencyCode = $formatter->getSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL); + } } parent::init(); @@ -257,15 +265,20 @@ class Formatter extends \yii\base\Formatter * Formats the value as a currency number. * @param mixed $value the value to be formatted * @param string $currency the 3-letter ISO 4217 currency code indicating the currency to use. + * If null, [[currencyCode]] will be used. * @param string $format the format to be used. Please refer to [ICU manual](http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details) * for details on how to specify a format. * @return string the formatted result. */ - public function asCurrency($value, $currency = 'USD', $format = null) + public function asCurrency($value, $currency = null, $format = null) { if ($value === null) { return $this->nullDisplay; } + + if ($currency === null){ + $currency = $this->currencyCode; + } return $this->createNumberFormatter(NumberFormatter::CURRENCY, $format)->formatCurrency($value, $currency); }