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

[10.x] Add localization support to the Number::forHumans helper #49106

Conversation

caendesilva
Copy link
Contributor

Abstract

Many people have requested a way to add localization to the forHumans helper added in #48845. Here's my attempt at implementing localization support for this method using translation helpers.

Example

// Example language setup

'sv' => [
    'thousand' => 'tusen',
    'million' => 'miljon',
    'billion' => 'miljard',
    'trillion' => 'biljon',
    'quadrillion' => 'biljard',
],

// Usage

Number::forHumans(1000, locale: 'sv'); // 1 tusen
Number::forHumans(10000, locale: 'sv'); // 10 tusen
Number::forHumans(1000000, locale: 'sv'); // 1 miljon

This has been requested by a large number of people after laravel#48845. My attempt at implementing localization support for this method is using translation helpers.

```php
// Example language setup

'sv' => [
    'thousand' => 'tusen',
    'million' => 'miljon',
    'billion' => 'miljard',
    'trillion' => 'biljon',
    'quadrillion' => 'biljard',
],
```
@caendesilva caendesilva force-pushed the number-for-humans-helper-localization-support branch from bb85881 to c3b7ba1 Compare November 23, 2023 19:59
I just want to preface this with saying that language is complex. Like really complex. It's doubtful that a relatively short and simple method can contain all the edge cases needed for full support of something as complex as this. I totally understand if this is not something that's worth merging, but I wanted to try it out so here it is. We can always revert the commit.
@caendesilva caendesilva force-pushed the number-for-humans-helper-localization-support branch from 97b1b0a to d08a707 Compare November 23, 2023 20:15
$this->assertSame('1,23 miljon', Number::forHumans(1234567, locale: 'sv', precision: 2));
}

public function testToHumansWithPluralizedLocalization()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just want to preface this with saying that language is complex. Like really complex. It's doubtful that a relatively short and simple method can contain all the edge cases needed for full support of something as complex as this. I totally understand if this is not something that's worth merging, but I wanted to try it out so here it is. We can always revert the commit.

}

$numberExponent = floor(log10($number));
$displayExponent = $numberExponent - ($numberExponent % 3);
$number /= pow(10, $displayExponent);

return trim(sprintf('%s %s', static::format($number, $precision, $maxPrecision), $units[$displayExponent] ?? ''));
$units = [
3 => trans_choice('thousand', $number, locale: $locale ?? static::$locale),
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it right to use global helper methods inside of the framework?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question! I think it's alright as I see global translation helpers being used in Framework code like validation and pagination helpers. But let's keep this open until we get input from someone from the team!

@@ -141,30 +141,30 @@ public static function fileSize(int|float $bytes, int $precision = 0, ?int $maxP
* @param int|null $maxPrecision
* @return string
*/
public static function forHumans(int|float $number, int $precision = 0, ?int $maxPrecision = null)
public static function forHumans(int|float $number, int $precision = 0, ?int $maxPrecision = null, ?string $locale = null)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this feature already released and class not final — this is a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure it is. Generally adding parameters to a method signature is not considered breaking in my personal experience with SemVer. Furthermore, while only tangentially related, Laravel does not include named arguments in their backwards compatibility guidelines.

@caendesilva caendesilva marked this pull request as ready for review November 23, 2023 20:42
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

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.

3 participants