Search

Laravel 5 String Helpers: Pluralization and Strings

December 6, 2017 —John Koster

The plural helper method will attempt to return a plural version of the given $value. It does this through a series of specialized internal functions, that will not be covered in great detail here. This helper method will apply general rules to strings ending in common character sequences, handle special cases and will not attempt some very specific strings. The change in a word, such as to show tense, or the number of items, is called an inflection.

doctrine/inflector

Previous versions of Laravel managed word inflections itself. Starting with version 5, Laravel now uses the doctrine/inflector package. This package is used by the Illuminate\Support\Pluralizer class, which is utilized by both the plural and the singular method.

#Signature

Te signature of the plural method is:

1public static function plural(
2 $value,
3 $count = 2
4 );

#Example Use

The method takes the $value that should be pluralized, and the $count of the items. If the $count is equal to 1, the original $value is returned.

1use Illuminate\Support\Str;
2 
3// cow
4echo Str::plural('cow', 1);
5 
6// cows
7echo Str::plural('cows');
8 
9// person
10echo Str::plural('person', 1);
11 
12// people
13echo Str::plural('person');

Assuming the following messages array:

1// The user's messages.
2$messages = [
3 'You have a meeting on Saturday.',
4 'Your inbox is 90% full',
5 'Do not eat yellow snow'
6];

We could display the number of messages to the user like so:

1use Illuminate\Support\Str;
2 
3$messageCount = count($messages);
4 
5echo 'You have '.$messageCount.' unread '.
6 Str::plural('message',$messageCount);

If the user had one message, the above example would output:

1You have 1 unread message.

Since the $messages array has 3 items in it, the user would see this:

1You have 3 unread messages.

#plural Special Cases

Any words, or word endings, in the following table will not be affected by the plural method. This is either because the resulting word is already plural, or because there is no inflection available. Word endings are denoted by the prefix *:

The following table lists the words that are not inflected by the plural method. This is either because the resulting word is already pluralized, or because there are no clear inflection rules available, or they handled separately (see the plural Special cases table).

Special Cases Special Cases Special Cases Special Cases
Amoyese bison Borghese bream
breeches britches buffalo cantus
carp chassis clippers code
caoitus Congoese contretemps corps
data debris diabetese djinn
eland elk equipment Faroese
flounder Foochowese Furniture gallows
Genevese Genoese Glibertese graffiti
headquarters herpes hijinks Hottentotese
information innings jackanapes Kiplingese
Kongoese Lucchese Luggage mackerel
Maltese *media mews moose
mumps Nankingese news nexus
Niasese Pekingese Piedmontese pincers
Postoiese pliers Portuguese proceedings
rabies rice rhinoceros salmon
Sarawakese scissors seabass sea-bass
series Shavese shears siemens
species staff swine testes
trousers trout tuna Vermontese
Wenchowese whitting wildebeest Yengeese

In addition to the general words that are not inflected by the plural method, the following tables list the word patterns or values that will also not be inflected by this method:

Words Not Inflected Words Not Inflected Words Not Inflected Words Not Inflected
*nese *rese *lese *mese
*deer *fish *measles *ois
*pox *sheep *ss people
cookie police

There are also many special cases handled by the plural method. These are generally words that do not have a clear pragmatic method of handling the process of converting the word to its plural equivalent:

Singular Form Plural Form
atlas atlases
axe axes
beef beefs
brother brothers
cafe cafes
chateau chateaux
niveau niveaux
child children
cookie cookies
corpus corpuses
cow cows
criterion criteria
curriculum curricula
demo demos
domino dominos
echo echoes
foot feet
fungus fungi
ganglion ganglions
genie genies
genus genera
graffito graffiti
hippopotamus hippopotami
hoof hoofs
human humans
iris irises
larva larvae
leaf leaves
loaf loaves
man men
medium media
memorandum memoranda
money monies
mongoose mongooses
motto mottoes
move moves
mythos mythoi
niche niches
nucleus nuclei
numen numina
occiput occiputs
octopus octopuses
opus opuses
ox oxen
passerby passersby
penis penises
person people
plateau plateaux
runner-up runners-up
sex sexes
soliloquy soliloquies
son-in-law sons-in-law
syllabus syllabi
testis testes
thief thieves
tooth teeth
tornado tornadoes
trilby trilbys
turf turfs
volcano volcanoes

#Global str_plural Helper Function

The str_plural function is a shortcut to calling Str::plural. This function is declared in the global namespace.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.