laravel-acronym-pluralizer maintained by sumaiazaman
Laravel Acronym Pluralizer
Fix Laravel's pluralizer for uppercase acronyms like CD, DVD, URL, API.
Before: Str::plural('CD') returns "CDS"
After: Str::plural('CD') returns "CDs"
The Problem
Laravel's Str::plural() uses Doctrine's inflector which correctly returns "CDs" from "CD". However, Laravel's matchCase() method then sees the input was all uppercase and runs mb_strtoupper() on the entire result, turning "CDs" into "CDS".
This package fixes matchCase() so that when the inflector preserves the original uppercase stem and only appends a lowercase suffix, the suffix remains lowercase.
Words that actually change form (like CHILD → CHILDREN, PERSON → PEOPLE) still get fully uppercased as before.
Installation
composer require sumaiazaman/laravel-acronym-pluralizer
Then add the following to your project's composer.json to swap out the original Pluralizer class:
{
"autoload": {
"exclude-from-classmap": [
"vendor/laravel/framework/src/Illuminate/Support/Pluralizer.php"
]
}
}
Finally, rebuild the autoloader:
composer dump-autoload
That's it. No service providers, no config files. Str::plural('CD') will now return "CDs" everywhere in your application.
How It Works
This package provides a drop-in replacement for Illuminate\Support\Pluralizer with a single fix in the matchCase() method. By using Composer's exclude-from-classmap, the original class is excluded from autoloading and this package's version is loaded instead.
The fix is minimal — when the input is all uppercase and the inflector preserved the original word exactly (just appended a suffix), we keep the original uppercase portion and leave the appended suffix as-is:
if ($function === 'mb_strtoupper' && str_starts_with($value, $comparison)) {
return $comparison.mb_substr($value, mb_strlen($comparison));
}
Examples
| Input | Before | After |
|---|---|---|
Str::plural('CD') |
CDS |
CDs |
Str::plural('DVD') |
DVDS |
DVDs |
Str::plural('URL') |
URLS |
URLs |
Str::plural('API') |
APIS |
APIs |
Str::plural('CAR') |
CARS |
CARs |
Str::plural('CHILD') |
CHILDREN |
CHILDREN |
Testing
composer test
Related
- laravel/framework#56932 — Original issue
- laravel/framework#59094 — Original PR
License
The MIT License (MIT). Please see License File for more information.