FText::Format does not work correctly with FText::AsNumber and NSLOCTEXT

Dear Friends at Epic,

#Repro

Try printing something like this using CanvasTextItem:

DayCount = FText::Format(NSLOCTEXT(“Solus”,“Solus”,"Day "), FText::AsNumber(1));

drawing to screen:

USolusCore::DrawJoyText(
	Canvas,
	Font,
	DayCount, 
	Blue,
	Origin.X, YOffset, 
	Scale,
	false//Outline
);

my function

static FORCEINLINE void DrawJoyText(
		FCanvas* Canvas,
		UFont* TheFont,
		const FText& TheText, 
		const FLinearColor& TheColor,
		const float& X, const float& Y, 
		const float& TheScale,
		bool DrawOutline=false,
		const FLinearColor OutlineColor=FLinearColor(0,0,0,1)
	) {
		if(!Canvas) return;
		//~~~~~~~~~
		
		//Text and Font
		FCanvasTextItem NewText(
			FVector2D(X,Y),
			TheText,
			TheFont,
			TheColor
		);
		
		//Text Scale
		NewText.Scale.Set(TheScale,TheScale);
		
		//Outline gets its alpha from the main color
		NewText.bOutlined = DrawOutline;
		NewText.OutlineColor = OutlineColor;
	
		//Draw
		Canvas->DrawItem(NewText);
	}

#Issue

Only the NSLOCTEXT prints to screen

but if I just use the number by itself, it prints fine:

DayCount = FText::AsNumber(1);

#Request

Please investigate my usage case above and see why the number is not showing up when used with FText::Format

DayCount = FText::Format(NSLOCTEXT("Solus","Solus","Day "), FText::AsNumber(1));

Sorry, not a bug :slight_smile:

The first parameter needs to be a format string specifier. I’ve commented more on your original question about this: How can I Concatenate FText Together? - Programming & Scripting - Epic Developer Community Forums

The first string needs to be a format specifier, you can also include text in this if you need to (just like with printf formatting), so you probably want something like this (the syntax is more like the Python str.format style to support re-ordering for localisation):

FText::Format(NSLOCTEXT("Solus","DayFmt","{0} {1}"), NSLOCTEXT("Solus","Solus","Day"), FText::AsNumber(DayCount)); // takes both as arguments
FText::Format(NSLOCTEXT("Solus","Solus","Day {0}"), FText::AsNumber(DayCount)); // takes just the day number and formats it into the Day string

You can also do the formatting as key->value pairs of formatting arguments:

FFormatNamedArguments Args;
Args.Add("DayCount", DayCount);
FText::Format(NSLOCTEXT("Solus","Day","Day {DayCount}"), Args);

Or alternatively, as an array of arguments:

FFormatOrderedArguments Args;
Args.Add(DayCount);
FText::Format(NSLOCTEXT("Solus","Day","Day {0}"), Args);
1 Like

#Exact Output

This:

DayCount 	=	FText::Format(NSLOCTEXT("Solus","Solus","Day "), FText::AsNumber(SaveDetails.DayCount));

when sent to my drawing function above,

yields this:

Whats odd is the second appearance of the NSLOCTEXT Day, this only happens when using FText::Format as above, it is not in my code.

#commentary on picture

no number for day, and day appears twice, using FText::Format as above

#Thank You Jamie!

Oh wow!

Thank you for all this info Jamie!

I am going to save this somewhere :slight_smile:

Thanks again!

#:heart:

Rama

I would expect the code you posted above to produce an output of "Day ", because you didn’t provide any formatting markers in the format specifier string (which was "Day "); the end result being you get out exactly what went in.

For format to work, it needs a format specifier to tell it where to place the arguments you’ve provided to it. It doesn’t just blindly concatenate things together in the order you passed them.

My answer goes into more detail about how format specifiers can be set-up.

#Wiki Tutorial Posted

Dear Jamie,

I have posted a wiki tutorial, thanks to your very helpful code samples:

#Thanks again!

#:heart:

Rama