Skip to content

Commit

Permalink
Merge pull request #6263 from kenjis/fix-format_number-locale
Browse files Browse the repository at this point in the history
fix: format_number() can't be used on CLI
  • Loading branch information
samsonasik committed Jul 14, 2022
2 parents c844ce1 + b2abfbf commit fe82ed1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
3 changes: 2 additions & 1 deletion system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Kint;
use Kint\Renderer\CliRenderer;
use Kint\Renderer\RichRenderer;
use Locale;
use LogicException;

/**
Expand Down Expand Up @@ -190,7 +191,7 @@ public function initialize()
}

// Set default locale on the server
locale_set_default($this->config->defaultLocale ?? 'en');
Locale::setDefault($this->config->defaultLocale ?? 'en');

// Set default timezone on the server
date_default_timezone_set($this->config->appTimezone ?? 'UTC');
Expand Down
7 changes: 3 additions & 4 deletions system/Helpers/number_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
* the LICENSE file that was distributed with this source code.
*/

use Config\Services;

// CodeIgniter Number Helpers

if (! function_exists('number_to_size')) {
Expand Down Expand Up @@ -129,8 +127,9 @@ function number_to_currency(float $num, string $currency, ?string $locale = null
*/
function format_number(float $num, int $precision = 1, ?string $locale = null, array $options = []): string
{
// Locale is either passed in here, negotiated with client, or grabbed from our config file.
$locale ??= Services::request()->getLocale();
// If locale is not passed, get from the default locale that is set from our config file
// or set by HTTP content negotiation.
$locale ??= Locale::getDefault();

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

0 comments on commit fe82ed1

Please sign in to comment.