Skip to content

[13.x] Add locale parameter to Number::fileSize(), forHumans(), and abbreviate()#59737

Closed
sumaiazaman wants to merge 2 commits intolaravel:13.xfrom
sumaiazaman:feat/number-locale-consistency
Closed

[13.x] Add locale parameter to Number::fileSize(), forHumans(), and abbreviate()#59737
sumaiazaman wants to merge 2 commits intolaravel:13.xfrom
sumaiazaman:feat/number-locale-consistency

Conversation

@sumaiazaman
Copy link
Copy Markdown
Contributor

Summary

  • Number::fileSize(), Number::forHumans(), and Number::abbreviate() now accept an optional ?string $locale = null parameter
  • The locale is threaded through to the internal Number::format() and Number::parseFloat() calls inside summarize()
  • All other formatting methods (format, percentage, currency, spell, ordinal, spellOrdinal, parse, parseInt, parseFloat) already supported this parameter — this closes the inconsistency

Context

Method locale param before locale param after
Number::format()
Number::percentage()
Number::currency()
Number::spell()
Number::ordinal()
Number::fileSize()
Number::forHumans()
Number::abbreviate()

Solution

Added ?string $locale = null as the last parameter to fileSize(), abbreviate(), forHumans(), and the internal summarize() method. The locale is forwarded to every static::format() and static::parseFloat() call within those methods.

Example

Before:

// No way to pass a per-call locale — had to use withLocale() wrapper
Number::withLocale('de', fn () => Number::fileSize(2048, precision: 2)); // '2,00 KB'
Number::withLocale('de', fn () => Number::abbreviate(1234, precision: 2)); // '1,23K'

After:

Number::fileSize(2048, precision: 2, locale: 'de');    // '2,00 KB'
Number::abbreviate(1234, precision: 2, locale: 'de');  // '1,23K'
Number::forHumans(1234, precision: 2, locale: 'fr');   // '1,23 thousand'

Why no breaking changes

All three parameters are appended at the end with a default of null, preserving full backwards compatibility. Existing calls are unaffected.

Changes

  • src/Illuminate/Support/Number.php — added ?string $locale = null to fileSize(), abbreviate(), forHumans(), and summarize()
  • tests/Support/SupportNumberTest.php — added locale assertions to testBytesToHuman and testSummarize

Test Plan

  • php83 vendor/bin/phpunit tests/Support/SupportNumberTest.php — all 22 tests pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants