更改

跳到导航 跳到搜索
建立内容为“← 目录 {{翻译}} 本页面描述了 '''模板字符串''',即一种内置的构建显示文本的方法,该文本可以包含任意…”的新页面
← [[模组:目录|目录]]

{{翻译}}

本页面描述了 '''模板字符串''',即一种内置的构建显示文本的方法,该文本可以包含任意组合的纯文本、翻译和占位符值。

==格式==
===综述===
您只能在允许使用模板字符串的数据字段中使用模板字符串(这些数据字段会在本百科文档中给出)。

一个模板字符串可以包括纯文本和[[#标记|下述标记]]的任意组合。例如:

<syntaxhighlight lang="js">
"Dialogue": "欢迎光临皮埃尔的杂货店![FarmName]最近怎么样?"
</syntaxhighlight>

当使用[[模组:Content Patcher|Content Patcher]]时,你可以在字符串的任何位置使用其标记(包括方括号内部);它们将在游戏解析字符串之前完全展开。例如,<code><nowiki>"{{Spouse}} 会喜欢 [ArticleFor [SuggestedItem]] [SuggestedItem]!"</nowiki></code>会输出"''Abigail would love an Apple!''"(此处放置英文原文便于理解)。

===标记格式===

“标记”是预定义的、用于生成文本的占位符,它们包裹在方括号中。标记名称并不大小写敏感,所以<samp>[LocalizedText]</samp>和<samp>[LOCALIZEDTEXT]</samp>是相同的标记。

例如,下面内容会在英语环境下生成"''Welcome to Pierre's! This is raw text''":

<syntaxhighlight lang="js">
"Dialogue": "[LocalizedText Strings\StringsFromCSFiles:ShopMenu.cs.11488] {{i18n: some-translation}}"
</syntaxhighlight>

===标记参数格式===

标记可以包含参数(参数也可包含标记)。在上面的例子中,<samp>LocalizedText</samp>具有一个参数(欲显示的翻译键)。参数使用空格分隔,因此需要使用<samp>EscapedText</samp>传入包含空格的参数:

<syntaxhighlight lang="js">
"Dialogue": "[LocalizedText [EscapedText Strings\BundleNames:Quality Fish]]"
</syntaxhighlight>

==标记==

下面列出了原版游戏支持的标记:
{| class="wikitable"
|-
! 标记格式
! 输出
|-
| <samp>[AchievementName {{t|id}}]</samp>
| 某个成就在当前语言下的名称。成就参见[[Modding:Achievement data|<samp>Data/Achievements</samp>]]。例如,<samp>[AchievementName 5]</samp>会在英语环境下输出''A Complete Collection''。
|-
| <samp>[ArticleFor {{t|word}}]</samp>
| 英语中的不定冠词(''a'' or ''an'')。在其他语言环境下为空白。 例如,<code>[ArticleFor apple] apple</code>输出''an apple''。
|-
| <samp>[CharacterName {{t|name}}]</samp>
| 根据角色的内部名称显示其在当前语言下的名词。
|-
| <samp>[DayOfMonth]</samp>
| 显示当月几日,例如在春5日显示<samp>5</samp>。
|-
| <samp>[EscapedText]</samp><br /><samp>[EscapedText {{t|text}}]</samp>
| 将文本片段视为单个参数,所以即使该片段为空或包含空格,也能被安全地传入其他标记中。

例如,<samp>SpouseFarmerText</samp>需要空格隔开的两个参数。下面的代码将向第一个参数传入空字符串,向第二个参数传入含空格的文本:
<pre>[SpouseFarmerText [EscapedText] [EscapedText spouse 28 63 2]]</pre>
|-
| <samp>[FarmerUniqueID]</samp>
| 目标玩家唯一的内部多人ID。
|-
| <samp>[FarmName]</samp>
| 当前存档的农场名(不包含后面的“农场”二字)
|-
| <samp>[FarmerStat {{t|key}}]</samp>
| 当前玩家的被跟踪的状态。欲获得详细状态列表,参见[[Modding:Game state queries|<samp>PLAYER_STAT</samp> game state query]]。

例如:
<syntaxhighlight lang="js">"你目前已经走了[FarmerStat stepsTaken]步了。"</syntaxhighlight>
|-
| <samp>[GenderedText {{t|male text}} {{t|female text}}]</samp>
| 根据玩家的性别显示不同文本。若包含空格,须使用<samp>EscapedText</samp>。
|-
| <samp>[ItemName {{t|id}} {{o|fallback text}}]</samp>
| 根据一个物品的限定ID或非限定ID,显示其在当前语言下的物品名称。例如,<samp>[ItemName (O)128]</samp>会在中文语境下显示“河豚”。

若该物品不存在,且指定了{{o|fallback text}}参数,则输出该参数。否则输出''Error Item ({{t|id}})''。
|-
| <samp>[LocalizedText {{t|string key}}]</samp><br /><samp>[LocalizedText {{t|string key}} {{t|token values}}+]</samp>
| 通过[[模组:常见的数据字段#翻译键|翻译键]]给出翻译后的文本。如果翻译中包含诸如<samp>{0}</samp>的占位符,您可以在字符串键后面添加其值。
|-
| <samp>[LocationName {{t|location ID}}]</samp>
| The translated display name for a location given its ID [[Modding:location data|in <samp>Data/Locations</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.
|-
| <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>
| 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>
| The translated display name for a special order from [[Modding:Special orders|<samp>Data/SpecialOrders</samp>]]. If the special order is currently active, it'll show the same name shown in the quest log. For example, <samp>[SpecialOrder Caroline]</samp> will output ''Island Ingredients'' in English.
|-
| <samp>[SpouseFarmerText {{t|spouse is farmer text}} {{t|spouse is NPC text}}</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>
| Equivalent to <samp>GenderedText</samp>, but based on the gender of the player's NPC or player spouse instead.
|-
| <samp>[SuggestedItem {{o|interval}} {{o|sync key}}]</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.
|-
| <samp>[ToolName {{t|id}} {{o|upgrade level}}]</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.
|}

==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.
* 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:模组]]

[[en:Modding:]]
39

个编辑

导航菜单