-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Process VariantTooltip at build time
Introduce `VariantTooltipProcessor`, which `LangTask`s use to process `VariantTooltip`s. This allows the runtime implementation to be greatly simplified, it now only handles differences in line `count`.
- Loading branch information
1 parent
19c8191
commit 4d2f91c
Showing
5 changed files
with
54 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class VariantTooltipProcessor : LangProcessor { | ||
private val variantRegex = "\\.@(?<variant>[^.]+)Tooltip(?<index>\\[\\d+])?${'$'}".toRegex() | ||
|
||
override fun process( | ||
modID: String, | ||
variant: String, | ||
translations: Map<String, String>, | ||
fallback: Map<String, String>? | ||
): Map<String, String> { | ||
val map = translations.toMutableMap() | ||
// Iterate over fallback values, to ensure variant-tooltips aren't accidentally overridden due to missing translations | ||
fallback?.forEach { (key, _) -> | ||
variantRegex.find(key)?.let { result -> | ||
map.remove(key) | ||
map.remove(baseKey(key, result)) | ||
} | ||
} | ||
// Then overwrite with actual values | ||
translations.forEach { (key, value) -> | ||
variantRegex.find(key)?.let { result -> | ||
// This is normally handled by the first loop, but fallback is nullable... | ||
map.remove(key) | ||
|
||
// Add the variant translation | ||
if (variant == result.groups["variant"]?.value?.lowercase()) { | ||
map[baseKey(key, result)] = value | ||
} | ||
} | ||
} | ||
return map | ||
} | ||
|
||
private fun baseKey(variantKey: String, result: MatchResult): String { | ||
val index = result.groups["index"]?.value ?: "" | ||
return variantKey.replaceAfterLast('.', "@Tooltip${index}") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters