Can variables be used in LOCTEXT? Or, how to handle grammar in localization

You never want to concatenate localizable text in code, nor in blueprints. You should use FText::Format() or “Format Text” in BPs, and replace the inputs with markers that can be moved by the translator. This assumes item.name and target.name are FText properties below.

E.g.

FText::Format(LOCTEXT("PlaceItem", "Take the {0} and put it on {1}"), item.name, target.name);

This way, the translator can move {0} and {1} around as he pleases and it’ll turn out correctly. Note that this doesn’t work if the grammar of the sentence changes with the item or target, or count of them, that’s just a limitation of the engine (and pretty messy to get right even if supported).

If the sentence is complex, you can use named arguments:

FFormatNamedArguments Arguments;
Arguments.Add(TEXT("item"), item.name);
Arguments.Add(TEXT("target"), target.name);
FText::Format(LOCTEXT( "PlaceItem", "Take the {item} and put it on {target}"), Arguments);
3 Likes