Jump to content

Modding:Tokenizable strings: Difference between revisions

From Stardew Valley Wiki
imported>Margotbean
zh link
imported>Sunbeam135
No edit summary
 
(6 intermediate revisions by 4 users not shown)
Line 29: Line 29:


==Tokens==
==Tokens==
Here are the tokens provided by the base game:
Here are the tokens provided by the base game.
 
===Game data===
{| class="wikitable"
{| class="wikitable"
|-
|-
! token format
! token format
! output
! output
|-
| <samp>[AchievementName {{t|id}}]</samp>
| The translated display name for an achievement from [[Modding:Achievement data|<samp>Data/Achievements</samp>]]. For example, <samp>[AchievementName 5]</samp> will output ''A Complete Collection'' in English.
|-
| <samp>[ArticleFor {{t|word}}]</samp>
| The grammatical article (''a'' or ''an'') for the given word when playing in English, else blank. For example, <code>[ArticleFor apple] apple</code> will output ''an apple''.
|-
| <samp>[CharacterName {{t|name}}]</samp>
| The translated display name for an NPC, given their internal name.
|-
|-
| <samp>[DayOfMonth]</samp>
| <samp>[DayOfMonth]</samp>
| The numeric day of month, like <samp>5</samp> on spring 5.
| The numeric day of month, like <samp>5</samp> on spring 5.
|-
| <samp>[EscapedText]</samp><br /><samp>[EscapedText {{t|text}}]</samp>
| Marks a snippet of text as a single argument, so it can be safely passed into another token's space-delimited arguments even if it's empty or contains spaces.
For example, the <samp>SpouseFarmerText</samp> expects two arguments separated by spaces. This passes an empty string for the first one, and text containing spaces for the second:
<pre>[SpouseFarmerText [EscapedText] [EscapedText spouse 28 63 2]]</pre>
|-
|-
| <samp>[FarmerUniqueID]</samp>
| <samp>[FarmerUniqueID]</samp>
Line 60: Line 47:
|-
|-
| <samp>[FarmerStat {{t|key}}]</samp>
| <samp>[FarmerStat {{t|key}}]</samp>
| The value of a tracked stat for the current player. See the [[Modding:Game state queries|<samp>PLAYER_STAT</samp> game state query]] for a list of useful stats.
| The value of a [[Modding:Stats|tracked stat]] for the current player.


For example:
For example:
<syntaxhighlight lang="js">"You've walked [FarmerStat stepsTaken] so far."</syntaxhighlight>
<syntaxhighlight lang="js">"You've walked [FarmerStat stepsTaken] steps so far."</syntaxhighlight>
|-
| <samp>[Season]</samp>
| The current season name, like ''spring''.
|-
|-
| <samp>[GenderedText {{t|male text}} {{t|female text}}</samp>
| <samp>[SuggestedItem {{o|interval}} {{o|sync key}}]</samp>
| Depending on the target player's gender, show either the male text or female text. If the text contains spaces, you'll need to escape them using <samp>EscapedText</samp>.
| '''(For shops only.)''' The name of a random item currently available in the shop stock.
 
The result will be identical for all queries with the same {{o|sync key}} during the given {{o|interval}} (one of <samp>tick</samp>, <samp>day</samp>, <samp>season</samp>, <samp>year</samp>), including between players in multiplayer mode. If omitted, they default to <samp>day</samp> and the shop ID respectively.
|}
 
===Localization===
{| class="wikitable"
|-
! token format
! output
|-
| <samp>[ArticleFor {{t|word}}]</samp>
| The grammatical article (''a'' or ''an'') for the given word when playing in English, else blank. For example, <code>[ArticleFor apple] apple</code> will output ''an apple''.
|-
| <samp>[AchievementName {{t|id}}]</samp>
| The translated display name for an achievement from [[Modding:Achievement data|<samp>Data/Achievements</samp>]]. For example, <samp>[AchievementName 5]</samp> will output ''A Complete Collection'' in English.
|-
| <samp>[CharacterName {{t|name}}]</samp>
| The translated display name for an NPC, given their internal name.
|-
| <samp>[GenderedText {{t|male text}} {{t|female text}} {{o|other text}}]</samp>
| Depending on the target player's gender, show either the male, female, or other text. If the text contains spaces, you'll need to escape them using <samp>EscapedText</samp>. If the player's gender is neither male nor female, the game will try to user the other text; if not provided, it will fall back on the female text.
|-
|-
| <samp>[ItemName {{t|id}} {{o|fallback text}}]</samp>
| <samp>[ItemName {{t|id}} {{o|fallback text}}]</samp>
Line 72: Line 83:


If the item doesn't exist, it'll output {{o|fallback text}} if specified, else ''Error Item ({{t|id}})''.
If the item doesn't exist, it'll output {{o|fallback text}} if specified, else ''Error Item ({{t|id}})''.
|-
| <samp>[ItemNameWithFlavor {{t|flavor type}} {{t|flavor ID}}]</samp>
| The translated display name for a flavored item name, where...
* {{t|flavor type}} is one of <samp>AgedRoe</samp>, <samp>Bait</samp>, <samp>DriedFruit</samp>, <samp>DriedMushroom</samp>, <samp>Honey</samp>, <samp>Jelly</samp>, <samp>Juice</samp>, <samp>Pickle</samp>, <samp>Roe</samp>, <samp>SmokedFish</samp>, or <samp>Wine</samp>;
* {{t|flavor ID}} is the [[Modding:Common data field types#Item ID|qualified or qualified item ID]] for the flavor (e.g. the blueberry in blueberry wine).
|-
|-
| <samp>[LocalizedText {{t|string key}}]</samp><br /><samp>[LocalizedText {{t|string key}} {{t|token values}}+]</samp>
| <samp>[LocalizedText {{t|string key}}]</samp><br /><samp>[LocalizedText {{t|string key}} {{t|token values}}+]</samp>
Line 81: Line 97:
| <samp>[MovieName {{t|id}}]</samp>
| <samp>[MovieName {{t|id}}]</samp>
| The translated display name for a movie from [[Modding:Movie theater data|<samp>Data/Movies</samp>]]. For example, <samp>[MovieName spring_movie_0]</samp> will output ''The Brave Little Sapling'' in English.
| The translated display name for a movie from [[Modding:Movie theater data|<samp>Data/Movies</samp>]]. For example, <samp>[MovieName spring_movie_0]</samp> will output ''The Brave Little Sapling'' in English.
|-
| <samp>[NumberWithSeparators {{t|number}}]</samp>
| Format a number with thousands separators based on the current language. For example, <samp>[NumberWithSeparators 5000000]</samp> will output ''5,000,000'' in English.
|-
|-
| <samp>[PositiveAdjective]</samp>
| <samp>[PositiveAdjective]</samp>
| A random adjective from the <samp>Strings\Lexicon</samp> data asset's <samp>RandomPositiveAdjective_PlaceOrEvent</samp> entry.
| A random adjective from the <samp>Strings\Lexicon</samp> data asset's <samp>RandomPositiveAdjective_PlaceOrEvent</samp> entry.
|-
| <samp>[Season]</samp>
| The current season name, like ''spring''.
|-
|-
| <samp>[SpecialOrderName {{t|id}}]</samp>
| <samp>[SpecialOrderName {{t|id}}]</samp>
Line 97: Line 107:
| Show a different text depending on whether the target player's spouse is a player or NPC. If the text contains spaces, you'll need to escape them using <samp>EscapedText</samp>.
| Show a different text depending on whether the target player's spouse is a player or NPC. If the text contains spaces, you'll need to escape them using <samp>EscapedText</samp>.
|-
|-
| <samp>[SpouseGenderedText {{t|male text}} {{t|female text}}]</samp>
| <samp>[SpouseGenderedText {{t|male text}} {{t|female text}} {{o|other text}}]</samp>
| Equivalent to <samp>GenderedText</samp>, but based on the gender of the player's NPC or player spouse instead.
| Equivalent to <samp>GenderedText</samp>, but based on the gender of the player's spouse (NPC or another player) instead.
|-
|-
| <samp>[SuggestedItem {{o|interval}} {{o|sync key}}]</samp>
| <samp>[ToolName {{t|id}} {{o|upgrade level}}]</samp>
| '''(For shops only.)''' The name of a random item currently available in the shop stock.
| The translated display name for a tool, including its upgrade level if specified. For example, <samp>[ToolName (T)IridiumAxe]</samp> will output ''Iridium Axe'' in English.
|}
 
===String manipulation===
{| class="wikitable"
|-
! token format
! output
|-
| <samp>[CapitalizeFirstLetter {{t|text}}]</samp>
| The input text, with the first letter capitalized.
|-
| <samp>[EscapedText]</samp><br /><samp>[EscapedText {{t|text}}]</samp>
| Marks a snippet of text as a single argument, so it can be safely passed into another token's space-delimited arguments even if it's empty or contains spaces.


The result will be identical for all queries with the same {{o|sync key}} during the given {{o|interval}} (one of <samp>tick</samp>, <samp>day</samp>, <samp>season</samp>, <samp>year</samp>), including between players in multiplayer mode. If omitted, they default to <samp>day</samp> and the shop ID respectively.
For example, the <samp>SpouseFarmerText</samp> expects two arguments separated by spaces. This passes an empty string for the first one, and text containing spaces for the second:
<pre>[SpouseFarmerText [EscapedText] [EscapedText spouse 28 63 2]]</pre>
|-
|-
| <samp>[ToolName {{t|id}} {{o|upgrade level}}]</samp>
| <samp>[NumberWithSeparators {{t|number}}]</samp>
| The translated display name for a tool, including its upgrade level if specified. For example, <samp>[ToolName (T)IridiumAxe]</samp> will output ''Iridium Axe'' in English.
| Format a number with thousands separators based on the current language. For example, <samp>[NumberWithSeparators 5000000]</samp> will output ''5,000,000'' in English.
|}
|}


==Extensibility for C# mods==
==Extensibility for C# mods==
* C# mods can define custom tokens by calling <code>TokenParser.RegisterParser("tokenName", ...)</code>. To avoid conflicts, custom token names should apply the [[Modding:Common data field types#Unique string ID|unique string ID]] conventions.
* C# mods can define custom tokens by calling <code>TokenParser.RegisterParser("tokenName", ...)</code>. To avoid conflicts, custom token names should apply the [[Modding:Common data field types#Unique string ID|unique string ID]] conventions.
* The <samp>TokenStringBuilder</samp> class provides methods for creating token strings. For example, <code>TokenStringBuilder.ItemNameFor(item)</code> will produce a string like <code>[ItemName (O)128]</code> or <code>ItemNameWithFlavor SmokedFish (O)128]</code>.
* The <samp>TokenStringBuilder</samp> class provides methods for creating token strings. For example, <code>TokenStringBuilder.ItemNameFor(item)</code> will produce a string like <code>[ItemName (O)128]</code> or <code>[ItemNameWithFlavor SmokedFish (O)128]</code>.


[[Category:Modding]]
[[Category:Modding]]


[[ru:Модификации:Строки с токенами]]
[[zh:模组:模板字符串]]
[[zh:模组:模板字符串]]

Latest revision as of 19:49, 9 January 2026

← Index

This page documents tokenizable strings, a built-in way to build display text which can contain any combination of literal text, translations, and placeholder values.

Format

Overview

You can only use tokenizable strings in data fields that specifically allow them (which will be indicated in the wiki docs).

A tokenizable string can contain any combination of literal text and tokens (listed below). For example:

"Dialogue": "Welcome to Pierre's! How is [FarmName] doing?"

When using Content Patcher, you can use its tokens anywhere in the string (including within square brackets); they'll be expanded before the game parses the string. For example, "{{Spouse}} would love [ArticleFor [SuggestedItem]] [SuggestedItem]!" would output something like "Abigail would love an Apple!".

Token format

A token is a predefined placeholder which produces text, wrapped in square brackets. Token names are not case-sensitive, so [LocalizedText] and [LOCALIZEDTEXT] are equivalent.

For example, this will show a message like "Welcome to Pierre's! This is raw text":

"Dialogue": "[LocalizedText Strings\StringsFromCSFiles:ShopMenu.cs.11488] {{i18n: some-translation}}"

Token argument format

A token can optionally have arguments (which can in turn contain tokens). In the above example, the LocalizedText takes one argument (the translation key to display). Arguments are space-delimited, so you need to use EscapedText to pass an argument containing spaces:

"Dialogue": "[LocalizedText [EscapedText Strings\BundleNames:Quality Fish]]"

Tokens

Here are the tokens provided by the base game.

Game data

token format output
[DayOfMonth] The numeric day of month, like 5 on spring 5.
[FarmerUniqueID] The target player's unique internal multiplayer ID
[FarmName] The farm name for the current save (without the injected "Farm" text).
[FarmerStat <key>] The value of a tracked stat for the current player.

For example:

"You've walked [FarmerStat stepsTaken] steps so far."
[Season] The current season name, like spring.
[SuggestedItem [interval] [sync key]] (For shops only.) The name of a random item currently available in the shop stock.

The result will be identical for all queries with the same [sync key] during the given [interval] (one of tick, day, season, year), including between players in multiplayer mode. If omitted, they default to day and the shop ID respectively.

Localization

token format output
[ArticleFor <word>] The grammatical article (a or an) for the given word when playing in English, else blank. For example, [ArticleFor apple] apple will output an apple.
[AchievementName <id>] The translated display name for an achievement from Data/Achievements. For example, [AchievementName 5] will output A Complete Collection in English.
[CharacterName <name>] The translated display name for an NPC, given their internal name.
[GenderedText <male text> <female text> [other text]] Depending on the target player's gender, show either the male, female, or other text. If the text contains spaces, you'll need to escape them using EscapedText. If the player's gender is neither male nor female, the game will try to user the other text; if not provided, it will fall back on the female text.
[ItemName <id> [fallback text]] The translated display name for an item based on its qualified or unqualified item ID. For example, [ItemName (O)128] will output Pufferfish in English.

If the item doesn't exist, it'll output [fallback text] if specified, else Error Item (<id>).

[ItemNameWithFlavor <flavor type> <flavor ID>] The translated display name for a flavored item name, where...
  • <flavor type> is one of AgedRoe, Bait, DriedFruit, DriedMushroom, Honey, Jelly, Juice, Pickle, Roe, SmokedFish, or Wine;
  • <flavor ID> is the qualified or qualified item ID for the flavor (e.g. the blueberry in blueberry wine).
[LocalizedText <string key>]
[LocalizedText <string key> <token values>+]
Translation text loaded from the given translation key. If the translation has placeholder tokens like {0}, you can add the values after the string key.
[LocationName <location ID>] The translated display name for a location given its ID in Data/Locations.
[MovieName <id>] The translated display name for a movie from Data/Movies. For example, [MovieName spring_movie_0] will output The Brave Little Sapling in English.
[PositiveAdjective] A random adjective from the Strings\Lexicon data asset's RandomPositiveAdjective_PlaceOrEvent entry.
[SpecialOrderName <id>] The translated display name for a special order from Data/SpecialOrders. If the special order is currently active, it'll show the same name shown in the quest log. For example, [SpecialOrder Caroline] will output Island Ingredients in English.
[SpouseFarmerText <spouse is farmer text> <spouse is NPC text> Show a different text depending on whether the target player's spouse is a player or NPC. If the text contains spaces, you'll need to escape them using EscapedText.
[SpouseGenderedText <male text> <female text> [other text]] Equivalent to GenderedText, but based on the gender of the player's spouse (NPC or another player) instead.
[ToolName <id> [upgrade level]] The translated display name for a tool, including its upgrade level if specified. For example, [ToolName (T)IridiumAxe] will output Iridium Axe in English.

String manipulation

token format output
[CapitalizeFirstLetter <text>] The input text, with the first letter capitalized.
[EscapedText]
[EscapedText <text>]
Marks a snippet of text as a single argument, so it can be safely passed into another token's space-delimited arguments even if it's empty or contains spaces.

For example, the SpouseFarmerText expects two arguments separated by spaces. This passes an empty string for the first one, and text containing spaces for the second:

[SpouseFarmerText [EscapedText] [EscapedText spouse 28 63 2]]
[NumberWithSeparators <number>] Format a number with thousands separators based on the current language. For example, [NumberWithSeparators 5000000] will output 5,000,000 in English.

Extensibility for C# mods

  • C# mods can define custom tokens by calling TokenParser.RegisterParser("tokenName", ...). To avoid conflicts, custom token names should apply the unique string ID conventions.
  • The TokenStringBuilder class provides methods for creating token strings. For example, TokenStringBuilder.ItemNameFor(item) will produce a string like [ItemName (O)128] or [ItemNameWithFlavor SmokedFish (O)128].