tModLoader v2024.03
A mod to make and play Terraria mods
LocalizedText Class Reference

Contains the localization value corresponding to a key for the current game language. Automatically updates as language, mods, and resource packs change. The Localization Guideteaches more about localization. More...

Public Member Functions

bool CanFormatWith (object obj)
 Checks if the conditions embedded in this LocalizedText are satisfied by the obj argument. For example when used with Lang.CreateDialogSubstitutionObject(NPC) as the obj argument, "{?Rain}" at the start of a LocalizedText value will cause false to be returned if it is not raining. "{?!Rain}" would do the opposite. If all conditions are satisfied, true is returned.
The method is typically used indirectly by using Lang.CreateDialogFilter(string, object). More...
 
string Format (params object[] args)
 Creates a string from this LocalizedText populated with data from the provided args arguments. Formats the string in the same manner as string.Format. Placeholders such as "{0}", "{1}", etc will be replaced with the string representation of the corresponding objects provided.
Additionally, pluralization is supported as well. The Contributing Localization Guideteaches more about placeholders and plural support. More...
 
string FormatWith (object obj)
 Creates a string from this LocalizedText populated with data from the provided obj parameter. The properties of the provided object are substituted by name into the placeholders of the original text. For example, when used with Lang.CreateDialogSubstitutionObject(NPC), the text "{Nurse}" will be replaced with the first name of the Nurse in the world. Modded substitutions are not currently supported.

When used in conjunction with Language.SelectRandom(LanguageSearchFilter, Utilities.UnifiedRandom) and Lang.CreateDialogFilter(string, object), simple boolean conditions expressed in each LocalizedText can be used to filter a collection of LocalizedText.

Format(object[]) is more commonly used to format LocalizedText placeholders. That method replaces placeholders such as "{0}", "{1}", etc with the string representation of the corresponding objects provided. More...
 
NetworkText ToNetworkText ()
 Convert this LocalizedText to a NetworkText for use in various network code applications. Non-chat messages sent to other players should be sent as NetworkText to facilitate localization.
 
NetworkText ToNetworkText (params object[] substitutions)
 
override string ToString ()
 
LocalizedText WithFormatArgs (params object[] args)
 Creates a new LocalizedText with the supplied arguments formatted into the value (via string.Format(string, object?[]))
Will automatically update to re-format the string with cached args when language changes.
br/> The resulting LocalizedText should be stored statically. Should not be used to create 'throwaway' LocalizedText instances.
Use Format(object[]) instead for repeated on-demand formatting with different args.
The Localization Guideteaches more about using placeholders in localization. More...
 

Static Public Member Functions

static string ApplyPluralization (string value, params object[] args)
 
static int CardinalPluralRule (GameCulture culture, int count)
 
static operator string (LocalizedText text)
 

Public Attributes

readonly string Key
 

Static Public Attributes

static readonly LocalizedText Empty = new LocalizedText("", "")
 
static readonly Regex PluralizationPatternRegex = new Regex(@"{\^(\d+):([^\r\n]+?)}", RegexOptions.Compiled)
 

Properties

object[] BoundArgs [get]
 Returns the args used with WithFormatArgs to create this text, if any.
 
string Value [get]
 Retrieves the text value. This is the actual text the user should see.
 

Detailed Description

Contains the localization value corresponding to a key for the current game language. Automatically updates as language, mods, and resource packs change. The Localization Guide

teaches more about localization.

Member Function Documentation

◆ CanFormatWith()

bool LocalizedText.CanFormatWith ( object  obj)

Checks if the conditions embedded in this LocalizedText are satisfied by the obj argument. For example when used with Lang.CreateDialogSubstitutionObject(NPC) as the obj argument, "{?Rain}" at the start of a LocalizedText value will cause false to be returned if it is not raining. "{?!Rain}" would do the opposite. If all conditions are satisfied, true is returned.
The method is typically used indirectly by using Lang.CreateDialogFilter(string, object).

Parameters
obj
Returns

◆ Format()

string LocalizedText.Format ( params object[]  args)

Creates a string from this LocalizedText populated with data from the provided args arguments. Formats the string in the same manner as string.Format. Placeholders such as "{0}", "{1}", etc will be replaced with the string representation of the corresponding objects provided.
Additionally, pluralization is supported as well. The Contributing Localization Guideteaches more about placeholders and plural support.

Parameters
args
Returns

◆ FormatWith()

string LocalizedText.FormatWith ( object  obj)

Creates a string from this LocalizedText populated with data from the provided obj parameter. The properties of the provided object are substituted by name into the placeholders of the original text. For example, when used with Lang.CreateDialogSubstitutionObject(NPC), the text "{Nurse}" will be replaced with the first name of the Nurse in the world. Modded substitutions are not currently supported.

When used in conjunction with Language.SelectRandom(LanguageSearchFilter, Utilities.UnifiedRandom) and Lang.CreateDialogFilter(string, object), simple boolean conditions expressed in each LocalizedText can be used to filter a collection of LocalizedText.

Format(object[]) is more commonly used to format LocalizedText placeholders. That method replaces placeholders such as "{0}", "{1}", etc with the string representation of the corresponding objects provided.

Parameters
obj
Returns

◆ WithFormatArgs()

LocalizedText LocalizedText.WithFormatArgs ( params object[]  args)

Creates a new LocalizedText with the supplied arguments formatted into the value (via string.Format(string, object?[]))
Will automatically update to re-format the string with cached args when language changes.
br/> The resulting LocalizedText should be stored statically. Should not be used to create 'throwaway' LocalizedText instances.
Use Format(object[]) instead for repeated on-demand formatting with different args.
The Localization Guideteaches more about using placeholders in localization.

Parameters
argsThe substitution args
Returns