Skip to content

Commit 9943061

Browse files
committed
add defaultKeys option for {{i18n}}
1 parent 6bcc38d commit 9943061

3 files changed

Lines changed: 47 additions & 12 deletions

File tree

ContentPatcher/Framework/Tokens/ValueProviders/TranslationValueProvider.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,28 @@ public override IEnumerable<string> GetValues(IInputArguments input)
6666
Translation translation = this.TranslationHelper
6767
.Get(key, tokens)
6868
.ApplyGenderSwitchBlocks(false); // preprocessing gender switch blocks doesn't work with patch update rates (e.g. NPCs won't update their dialogue once the save is loaded)
69+
bool hasValue = translation.HasValue();
70+
71+
// apply fallback keys
72+
if (!hasValue && input.NamedArgs.TryGetValue("defaultKeys", out IInputArgumentValue? defaultKeys))
73+
{
74+
foreach (string defaultKey in defaultKeys.Parsed)
75+
{
76+
Translation newTranslation = this.TranslationHelper
77+
.Get(defaultKey, tokens)
78+
.ApplyGenderSwitchBlocks(false);
79+
80+
if (newTranslation.HasValue())
81+
{
82+
translation = newTranslation;
83+
hasValue = true;
84+
break;
85+
}
86+
}
87+
}
6988

7089
// add default value
71-
if (input.NamedArgs.TryGetValue("default", out IInputArgumentValue? defaultValue))
90+
if (!hasValue && input.NamedArgs.TryGetValue("default", out IInputArgumentValue? defaultValue))
7291
{
7392
translation = translation
7493
.Default(this.Stringify(defaultValue))

ContentPatcher/docs/author-guide/translations.md

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,33 @@ specify these arguments to `i18n`:
3535
<td><code>default</code></td>
3636
<td>
3737

38-
If a translation doesn't exist (in both the current language _and_ `default.json`), the token will
39-
return text like "missing translation: key". You can provide a different default value using the
40-
`default` argument:
38+
If the translation doesn't exist (in both the current language _and_ `default.json`), the text to
39+
return instead of "_missing translation: key_".
40+
41+
For example:
4142
```js
4243
"{{i18n:some-key |default=default text to display}}"
4344
```
4445

45-
You can use tokens in the default text, so you can also default to a different translation:
46+
You can also use tokens in the default text:
47+
```js
48+
"{{i18n:some-key |default=Hi {{PlayerName}}! }}"
49+
```
50+
51+
</td>
52+
</tr>
53+
<tr>
54+
<td><code>defaultKeys</code></td>
55+
<td>
56+
57+
If the translation doesn't exist (in both the current language _and_ `default.json`), the
58+
translation keys to use instead. The first key which exists in the translation files is used.
59+
The other arguments (like `default`) are applied to the selected translation key.
60+
61+
For example, let's say your translation files only contain a `valid-key` translation. This code
62+
would return that translation:
4663
```js
47-
"{{i18n:some-key |default={{i18n:another-key}} }}"
64+
"{{i18n: missing-key |defaultKeys=missing-key-2, valid-key}}"
4865
```
4966

5067
</td>
@@ -59,7 +76,7 @@ _any other_
5976

6077
Any other arguments provide values for translation tokens (not case-sensitive). For example, if you
6178
have a translation like this:
62-
```json
79+
```js
6380
{
6481
"dialogue": "Hi {{name}}, it's a beautiful {{day}} morning!"
6582
}
@@ -147,10 +164,8 @@ which edits every festival in the game to add dynamic dialogue based on the tran
147164
"Target": "Data/Festivals/spring13, Data/Festivals/spring24, Data/Festivals/summer11, Data/Festivals/summer28, Data/Festivals/fall16, Data/Festivals/fall27, Data/Festivals/winter8, Data/Festivals/winter25",
148165
"Entries": {
149166
"Alexia": "
150-
{{i18n:festival-{{TargetWithoutPath}}.{{Relationship:Alexia}} |default=
151-
{{i18n:festival-{{TargetWithoutPath}} |default=
152-
{{i18n:festival-default}}
153-
}}
167+
{{i18n:festival-{{TargetWithoutPath}}.{{Relationship:Alexia}}
168+
|defaultKeys=festival-{{TargetWithoutPath}}, festival-default
154169
}}
155170
"
156171
}

ContentPatcher/docs/release-notes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ When releasing a format change, don't forget to update the smapi.io/json schema!
1010
1111
-->
1212
## Upcoming release
13-
* Added `AddNpcWarps` field in [`EditMap` patches](https://github.com/Pathoschild/StardewMods/blob/develop/ContentPatcher/author-guide.md#editmap).
13+
* Added `AddNpcWarps` field in [`EditMap` patches](author-guide.md#editmap).
14+
* Added `defaultKeys` option for [the `i18n` token](author-guide/translations.md).
1415
* Fixed some cases where a local token on an `Include` patch wasn't available in the included patches.
1516

1617
## 2.5.3

0 commit comments

Comments
 (0)