Choose your operating system:
Windows
macOS
Linux
In Unreal Engine 4 (UE4) the primary component for
text localization
is the
FText
class. All user-facing text should use this class, as it supports text localization by providing the following features:
-
Formatting Text (to generate text from a placeholder pattern).
-
Generating derived text , such as making text upper or lower case.
FText
also features the
AsCultureInvariant
function (or the
INVTEXT
macro), which creates non-localized, or "culture invariant" text. This is useful for things like converting a player name from an external API into something you can display in your user interface.
You can create a blank
FText
using either
FText::GetEmpty()
, or by using just
FText()
.
Conversion
FText
can be converted to and from
FString
. However, because
FText
contains character strings linked to localization data, while
FString
only contains a character string, these methods are inherently lossy and will discard (or fail to create) localization data. An alternative method,
Text Value Marshalling
provides lossless conversion, although this method produces data more suitable for internal marshalling than human consumption.
The following conversion functions produce
FText
strings, but without localization data:
|
Description |
---|---|
|
Creates a non-localized and culture invariant
|
|
Creates a non-localized
This is identical to
|
|
Creates a non-localized
|
To convert from
FText
to
FString
, use the
ToString
function. The resulting
FString
will hold the string data from the
FText
, but not the localization data.
Comparison
Because
FText
data is more complex than simple strings, it does not support overloaded-operator comparisons. Instead, it provides several functions to perform comparisons that recognize the nuanced data that it contains. The following comparison functions are available:
|
Description |
---|---|
|
This function takes an
|
|
This function is a wrapper for calling
|
|
This function takes an
|
|
This function is a wrapper for calling
|
Using FText in User Interfaces
Slate/UMG
Slate and UMG use
FText
attributes or arguments for any built-in Widgets that show or manage user-facing text. We recommend using
FText
for any custom Widgets that you build.
HUD/Canvas
To display
FText
through the HUD system with Canvas, create a new
FCanvasTextItem
and set its
Text
variable to the text you want to display, as in the following example code:
// Create a new FCanvasTextItem instance to contain the text.
FCanvasTextItem TextItem(FVector2D::ZeroVector, TestHUDText, BigFont, FLinearColor::Black);
// Add the text into the FCanvasTextItem.
TextItem.Text = FText::Format(LOCTEXT("ExampleFText", "You currently have {0} health left."), CurrentHealth);
// Draw the text to the screen with FCanvas::DrawItem.
Canvas->DrawItem(TextItem, 10.0f, 10.0f);
When using Canvas in a HUD, you must call
DrawItem
within the
DrawHUD
function, or call it in a function chain that begins with
DrawHUD
.