Pluralizing Item Names

When 'strawberrys' won't cut it.

Say you're making an cooking game, and in your game there are many ingredients: eggs, lemons, ants, acorns etc. You want to be able to show text like this:

"You added 3 acorns to the soup"

You have a bunch of item definitions with their names, but dang it they're all singular. You tried slapping an "s" on the end of the word but "strawberrys" isn't making the cut.

You've put all your terms in a localization table and you're loading it with C++ by using a handy tutorial

cooking-before.csv
Key,SourceString,Comment
Ingredient_Lemon_Name,"lemon",
Ingredient_Strawberry_Name,"strawberry",
Ingredient_Acorn_Name,"acorn",
Drop_Result,"You found {Count} {Ingredient}!",

Fixing Our Localization Table

First, we need to fix our locatization table to support both singular and plural names. It now looks like this:

cooking-after.csv
Ingredient_Lemon_Name,"{Count}|plural(one=lemon,other=lemons)",
Ingredient_Strawberry_Name,"{Count}|plural(one=strawberry,other=strawberries)",
Ingredient_Acorn_Name,"{Count}|plural(one=acorn,other=acorns)",
Drop_Result,"You found {Count} {Ingredient}!",

Adding a C++ Function

Next, we need to make a C++ function that we can use every time we want to display an item name. It will set up the Count variable we need to display the correct string.

MyGameplayStatics.cpp
FText UMyGameplayStatics::GetIngredientName( FString IngredientName, int32 Count )
{
	FString Key = FString::Printf( "Ingredient_%s_Name", *IngredientName );

	FText UnformattedText = FText::FromStringTable( "MyTable", Key );

	FFormatNamedArguments Args;
	Args.Add( "Count", Count );
	return FText::Format( UnformattedText, Args );
}

Now whenever we need to show an ingredient name we pass in how many we're showing, and we can get the correct plural form!

ExampleUse.cpp
FLootDrop Loot;
FFormatNamedArguments Args;
Args.Add( "Count", Loot.Count );
Args.Add( "Ingredient", Loot.Ingredient );

// Should say "3 strawerries" or "1 lemon", depending on our loot
FText LootText = FText::Format( FText::FromStringTable("MyTable", "Drop_Result"), Args ) );

LootDropLabel.SetText( LootText );