Format FText with Named Arguments in Unreal C++
Need to combine several FText values for an Unreal UI? Instead of concatenating strings, give each value a name and insert it into a format pattern with FText::Format.
For example, we can turn an ability name and description into this tooltip:
Fireball:
Deals fire damage to the target.Here is the complete localization-ready function:
FText UPassiveAbilityFactory::ToText() const
{
FFormatNamedArguments Args;
Args.Add(TEXT("ItemName"), GetAbilityName());
Args.Add(TEXT("ItemDescription"), GetDescription());
const FText Pattern = NSLOCTEXT(
"PassiveAbility",
"AbilityDescriptionFormat",
"{ItemName}:\n{ItemDescription}"
);
return FText::Format(Pattern, Args);
}Let's break down the three pieces that make it work.
1. Create the Named Arguments
Start by adding each value to an FFormatNamedArguments collection:
FFormatNamedArguments Args;
Args.Add(TEXT("ItemName"), GetAbilityName());
Args.Add(TEXT("ItemDescription"), GetDescription());Each argument has a name and a value:
ItemNamecontains the ability name.ItemDescriptioncontains the ability description.
Those names become placeholders in the format pattern. Using descriptive placeholders makes the relationship between the pattern and its values easier to understand than positional placeholders such as {0} and {1}.
GetAbilityName() and GetDescription() should return localization-ready
FText values when their content needs to be translated.
2. Define the Format Pattern
Next, describe how the final text should be arranged:
const FText Pattern = NSLOCTEXT(
"PassiveAbility",
"AbilityDescriptionFormat",
"{ItemName}:\n{ItemDescription}"
);The pattern contains the same names that were added to Args:
{ItemName}:
{ItemDescription}At runtime, Unreal replaces each placeholder with its matching value. The \n escape sequence puts the description on a new line.
NSLOCTEXT gives this player-facing pattern a stable localization identity:
- Namespace:
PassiveAbilitygroups related text. - Key:
AbilityDescriptionFormatidentifies this entry. - Source text:
{ItemName}:\n{ItemDescription}is the default pattern.
A translator can rearrange the named placeholders when another language needs a different word order without changing the C++ code.
3. Format the Final Text
Pass the pattern and arguments to FText::Format:
return FText::Format(Pattern, Args);For our Fireball ability, Unreal resolves the placeholders like this:
{ItemName} -> Fireball
{ItemDescription} -> Deals fire damage to the target.The final FText keeps the formatting and localization history Unreal needs for player-facing UI.
Common Mistakes
Concatenating player-facing strings
Building the sentence piece by piece makes it harder for translators to change its structure. Keep the complete arrangement in one localizable format pattern instead.
Mismatched placeholder names
The names in the pattern must match the names added to Args. For example, {Description} will not use an argument named ItemDescription.
Localizing only the pattern
The pattern and its inserted values are separate text. A localization-ready pattern does not automatically translate the ability name or description.
Reusing the same key for different text
Treat the namespace and key as the stable identity of this pattern. Use a different key when another piece of text has a different meaning or translation context.
The Takeaway
Use FFormatNamedArguments to give each value a descriptive placeholder, keep the complete player-facing structure in an NSLOCTEXT pattern, and combine them with FText::Format.
That gives you readable C++ and a format translators can safely rearrange.
For more detail, see Epic's FText documentation and Text Localization overview.