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

fix: format_number() can't be used on CLI #6263

Merged
merged 3 commits into from
Jul 14, 2022
Merged
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
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');
Comment on lines -193 to +194
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why you changed this. Isn't they the same?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we are using Locale::getDefault() and I searched Locale::setDefault() but not found.
Using locale_set_default() and Locale::getDefault() is not consistent.


// 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