Skip to content

Commit 129c1e6

Browse files
committed
add Translation method to disable gender-switch block handling
This allows for edge cases where a mod needs to keep gender-switch blocks as-is (e.g. to pass to dialogue later).
1 parent d779c7d commit 129c1e6

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

src/SMAPI/Translation.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public class Translation
3333
/// <summary>Whether to use <see cref="Placeholder"/> if the translation is <c>null</c> or empty.</summary>
3434
private bool ShouldUsePlaceholder { get; init; }
3535

36+
/// <summary>Whether to process gender-switch blocks in translation text, if any.</summary>
37+
private bool ShouldApplyGenderSwitchBlocks { get; init; }
38+
3639
/// <summary>The tokens to apply to the translation text, if any.</summary>
3740
private Dictionary<string, string?>? TokenValues { get; init; }
3841

@@ -64,6 +67,7 @@ internal Translation(string locale, string key, string? text)
6467
this.Text = text;
6568
this.Placeholder = string.Format(Translation.PlaceholderText, key);
6669
this.ShouldUsePlaceholder = true;
70+
this.ShouldApplyGenderSwitchBlocks = true;
6771
}
6872

6973
/// <summary>Construct an instance.</summary>
@@ -73,6 +77,7 @@ internal Translation(Translation other)
7377
{
7478
this.Placeholder = other.Placeholder;
7579
this.ShouldUsePlaceholder = other.ShouldUsePlaceholder;
80+
this.ShouldApplyGenderSwitchBlocks = other.ShouldApplyGenderSwitchBlocks;
7681
this.TokenValues = other.TokenValues;
7782
this.ForceGender = other.ForceGender;
7883
}
@@ -109,6 +114,20 @@ public Translation UsePlaceholder(bool use)
109114
};
110115
}
111116

117+
/// <summary>Whether to automatically process gender-switch blocks (i.e. <see cref="Dialogue.applyGenderSwitchBlocks"/>) before the text is returned.</summary>
118+
/// <param name="apply">Whether to process gender-switch blocks.</param>
119+
/// <remarks>Returns a new instance if this would change the result, else the current instance.</remarks>
120+
public Translation ApplyGenderSwitchBlocks(bool apply)
121+
{
122+
if (this.ShouldApplyGenderSwitchBlocks == apply)
123+
return this;
124+
125+
return new Translation(this)
126+
{
127+
ShouldApplyGenderSwitchBlocks = apply
128+
};
129+
}
130+
112131
/// <summary>Replace tokens in the text like <c>{{value}}</c> with the given values.</summary>
113132
/// <param name="tokens">An object containing token key/value pairs. This can be an anonymous object (like <c>new { value = 42, name = "Cranberries" }</c>), a dictionary, or a class instance.</param>
114133
/// <exception cref="ArgumentNullException">The <paramref name="tokens"/> argument is <c>null</c>.</exception>

0 commit comments

Comments
 (0)