Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/localization number filters #739

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 33 additions & 30 deletions system/Config/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,45 +27,48 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package CodeIgniter
* @author CodeIgniter Dev Team
* @copyright 2014-2017 British Columbia Institute of Technology (https://bcit.ca/)
* @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @since Version 3.0.0
* @package CodeIgniter
* @author CodeIgniter Dev Team
* @copyright 2014-2017 British Columbia Institute of Technology (https://bcit.ca/)
* @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @since Version 3.0.0
* @filesource
*/

class View extends BaseConfig
{

protected $coreFilters = [
'abs' => '\abs',
'capitalize' => '\CodeIgniter\View\Filters::capitalize',
'date' => '\CodeIgniter\View\Filters::date',
'date_modify' => '\CodeIgniter\View\Filters::date_modify',
'default' => '\CodeIgniter\View\Filters::default',
'esc' => '\CodeIgniter\View\Filters::esc',
'excerpt' => '\CodeIgniter\View\Filters::excerpt',
'highlight' => '\CodeIgniter\View\Filters::highlight',
'abs' => '\abs',
'capitalize' => '\CodeIgniter\View\Filters::capitalize',
'date' => '\CodeIgniter\View\Filters::date',
'date_modify' => '\CodeIgniter\View\Filters::date_modify',
'default' => '\CodeIgniter\View\Filters::default',
'esc' => '\CodeIgniter\View\Filters::esc',
'excerpt' => '\CodeIgniter\View\Filters::excerpt',
'highlight' => '\CodeIgniter\View\Filters::highlight',
'highlight_code' => '\CodeIgniter\View\Filters::highlight_code',
'limit_words' => '\CodeIgniter\View\Filters::limit_words',
'limit_chars' => '\CodeIgniter\View\Filters::limit_chars',
'lower' => '\strtolower',
'nl2br' => '\CodeIgniter\View\Filters::nl2br',
'number_format' => '\number_format',
'prose' => '\CodeIgniter\View\Filters::prose',
'round' => '\CodeIgniter\View\Filters::round',
'strip_tags' => '\strip_tags',
'title' => '\CodeIgniter\View\Filters::title',
'upper' => '\strtoupper',
'limit_words' => '\CodeIgniter\View\Filters::limit_words',
'limit_chars' => '\CodeIgniter\View\Filters::limit_chars',
'local_number' => '\CodeIgniter\View\Filters::localizedNumber',
'currency' => '\CodeIgniter\View\Filters::currency',
'lower' => '\strtolower',
'nl2br' => '\CodeIgniter\View\Filters::nl2br',
'number_format' => '\number_format',
'prose' => '\CodeIgniter\View\Filters::prose',
'round' => '\CodeIgniter\View\Filters::round',
'strip_tags' => '\strip_tags',
'title' => '\CodeIgniter\View\Filters::title',
'upper' => '\strtoupper',
];
protected $corePlugins = [
'current_url' => '\CodeIgniter\View\Plugins::currentURL',
'previous_url' => '\CodeIgniter\View\Plugins::previousURL',
'mailto' => '\CodeIgniter\View\Plugins::mailto',
'safe_mailto' => '\CodeIgniter\View\Plugins::safeMailto',
'lang' => '\CodeIgniter\View\Plugins::lang',
'validation_errors' => '\CodeIgniter\View\Plugins::validationErrors',
'current_url' => '\CodeIgniter\View\Plugins::currentURL',
'previous_url' => '\CodeIgniter\View\Plugins::previousURL',
'mailto' => '\CodeIgniter\View\Plugins::mailto',
'safe_mailto' => '\CodeIgniter\View\Plugins::safeMailto',
'lang' => '\CodeIgniter\View\Plugins::lang',
'validation_errors' => '\CodeIgniter\View\Plugins::validationErrors',
];

public function __construct()
Expand Down
8 changes: 7 additions & 1 deletion system/Helpers/number_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,19 @@ function number_to_currency($num, string $currency, string $locale = null)
*/
function format_number($num, int $precision = 1, string $locale = null, array $options = [])
{
//First of all check if intl extention is available or not
if (! class_exists('IntlDateFormatter'))
{
throw new \RuntimeException(lang('Number.IntlNotFound'));
}

// Locale is either passed in here, negotiated with client, or grabbed from our config file.
$locale = $locale ?? \CodeIgniter\Config\Services::request()->getLocale();

// Type can be any of the NumberFormatter options, but provide a default.
$type = isset($options['type']) ? (int) $options['type'] :
NumberFormatter::DECIMAL;

// In order to specify a precision, we'll have to modify
// the pattern used by NumberFormatter.
$pattern = '#,##0.' . str_repeat('#', $precision);
Expand Down
152 changes: 152 additions & 0 deletions system/I18n/Number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php namespace CodeIgniter\I18n;

/**
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014-2017 British Columbia Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package CodeIgniter
* @author CodeIgniter Dev Team
* @copyright 2014-2017 British Columbia Institute of Technology (https://bcit.ca/)
* @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @since Version 3.0.0
* @filesource
*/
use Locale;
use DateTime;
use DateInterval;
use DateTimeZone;
use IntlDateFormatter;

/**
* Class Number
*
* A localized Number package inspired
* by Twig localized number extention
* todo: replace with proper name.
*
* Requires the intl PHP extension.
*
* @package CodeIgniter\I18n
*/

class Number
{
/**
* describe method
*
* @param $value
*
* @return string
*/
public static function localizedNumber($number, $style = 'decimal', $type = 'default', $locale = null):string
{
self::raiseExceptionIfIntlExtensionNotAvailable();
static $typeValues = [
'default' => \NumberFormatter::TYPE_DEFAULT,
'int32' => \NumberFormatter::TYPE_INT32,
'int64' => \NumberFormatter::TYPE_INT64,
'double' => \NumberFormatter::TYPE_DOUBLE,
'currency' => \NumberFormatter::TYPE_CURRENCY,
];

$formatter = self::getNumberFormatter($locale, $style);
if (! isset($typeValues[$type]))
{
throw new \Exception('syntax error');
//todo: raise mor appropriate /specific exception
}
return $formatter->format($number, $typeValues[$type]);
}

/**
* describe method
*
* @param $value
*
* @return string
*/
public static function localizedCurrency($number, $currency = null, $locale = null)
{
$locale = $locale ?? \CodeIgniter\Config\Services::request()->getLocale();

$formatter = self::getNumberFormatter($locale, 'currency');
return $formatter->formatCurrency($number, $currency);
}
//--------------------------------------------------------------------

/**
* describe method
*
* @param $value
*
* @return string
*/
private static function getNumberFormatter($locale, $style):\NumberFormatter
{
static $formatter, $currentStyle;

$locale = $locale ?? \CodeIgniter\Config\Services::request()->getLocale();

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 = [
'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 \Exception('syntax error');
//todo: raise more appropriate /specific exception
}
$currentStyle = $style;
$formatter = \NumberFormatter::create($locale, $styleValues[$style]);
return $formatter;
}
//--------------------------------------------------------------------

/**
* describe metho
*/
private static function raiseExceptionIfIntlExtensionNotAvailable()
{
if (! class_exists('IntlDateFormatter'))
{
throw new \RuntimeException('The intl extension is needed to use intl-based filters.');
}
}
//--------------------------------------------------------------------

}
1 change: 1 addition & 0 deletions system/Language/en/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@
'billion' => ' billion',
'trillion' => ' trillion',
'quadrillion' => ' quadrillion',
'IntlNotFound' => 'Intl extention is required',
];
Loading