diff --git a/system/Config/View.php b/system/Config/View.php index 539338cdf230..6741f0a7369e 100644 --- a/system/Config/View.php +++ b/system/Config/View.php @@ -50,6 +50,7 @@ class View extends BaseConfig 'highlight_code' => '\CodeIgniter\View\Filters::highlight_code', 'limit_words' => '\CodeIgniter\View\Filters::limit_words', 'limit_chars' => '\CodeIgniter\View\Filters::limit_chars', + 'local_currency' => '\CodeIgniter\View\Filters::local_currency', 'local_number' => '\CodeIgniter\View\Filters::local_number', 'lower' => '\strtolower', 'nl2br' => '\CodeIgniter\View\Filters::nl2br', diff --git a/system/View/Filters.php b/system/View/Filters.php index d096280b754e..c0078525d0d3 100644 --- a/system/View/Filters.php +++ b/system/View/Filters.php @@ -213,7 +213,7 @@ public static function limit_words($value, int $limit = 100): string //-------------------------------------------------------------------- /** - * Returns the $value displayed in the current + * Returns the $value displayed in a localized manner. * * @param $value * @param int $precision @@ -241,6 +241,27 @@ public static function local_number($value, string $type='decimal', $precision=4 //-------------------------------------------------------------------- + /** + * Returns the $value displayed as a currency string. + * + * @param $value + * @param string $currency + * @param string|null $locale + * + * @return string + */ + public static function local_currency($value, string $currency, string $locale = null): string + { + helper('number'); + + $options = [ + 'type' => \NumberFormatter::CURRENCY, + 'currency' => $currency + ]; + + return format_number($value, 2, $locale, $options); + } + /** * Returns a string with all instances of newline character (\n) * converted to an HTML
tag. diff --git a/tests/system/View/ParserFilterTest.php b/tests/system/View/ParserFilterTest.php index 0526b78725aa..7f1878fc5c2f 100644 --- a/tests/system/View/ParserFilterTest.php +++ b/tests/system/View/ParserFilterTest.php @@ -359,4 +359,19 @@ public function testLocalNumberLocale() $parser->setData($data); $this->assertEquals('1.234.567,8912', $parser->renderString($template)); } + + public function testLocalCurrency() + { + $parser = new Parser($this->config, $this->viewsDir, $this->loader); + + $data = [ + 'mynum' => 1234567.891234567890000 + ]; + + $template = '{ mynum|local_currency(EUR,de_DE) }'; + + $parser->setData($data); + $this->assertEquals('1.234.567,89 €', $parser->renderString($template)); + } + } diff --git a/user_guide_src/source/general/view_parser.rst b/user_guide_src/source/general/view_parser.rst index d8ec101a6f37..1581b35559de 100644 --- a/user_guide_src/source/general/view_parser.rst +++ b/user_guide_src/source/general/view_parser.rst @@ -403,6 +403,8 @@ highlight phrase Highlights a given phrase within highlight_code Highlights code samples with HTML/CSS. { v|highlight_code } limit_chars limit Limits the number of chracters to $limit. { v|limit_chars(100) } limit_words limit Limits the number of words to $limit. { v|limit_words(20) } +local_currency currency, locale Displays a localized version of a currency. "currency" value is any { v|local_currency(EUR,en_US) } + 3-letter ISO 4217 currency code. local_number type, precision, locale Displays a localized version of a number. "type" can be one of: { v|local_number(decimal,2,en_US) } decimal, currency, percent, scientific, spellout, ordinal, duration. See `PHP's NumberFormatter `_ for details.