mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-19 20:28:56 +07:00
Resolved #5188 - stat names also include the stat icon :)
This commit is contained in:
@ -3,6 +3,7 @@ package com.unciv.models.translations
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.unciv.UncivGame
|
||||
import com.unciv.models.ruleset.unique.Unique
|
||||
import com.unciv.models.stats.Stat
|
||||
import com.unciv.models.stats.Stats
|
||||
import java.util.*
|
||||
import kotlin.collections.HashMap
|
||||
@ -254,12 +255,12 @@ val pointyBraceRegex = Regex("""\<([^>]*)\>""")
|
||||
*/
|
||||
fun String.tr(): String {
|
||||
val activeMods = with(UncivGame.Current) {
|
||||
if (isGameInfoInitialized())
|
||||
gameInfo.gameParameters.mods + gameInfo.gameParameters.baseRuleset
|
||||
if (isGameInfoInitialized())
|
||||
gameInfo.gameParameters.mods + gameInfo.gameParameters.baseRuleset
|
||||
else translations.translationActiveMods
|
||||
}.toHashSet()
|
||||
val language = UncivGame.Current.settings.language
|
||||
|
||||
|
||||
if (contains('<')) { // Conditionals!
|
||||
/**
|
||||
* So conditionals can contain placeholders, such as <vs [unitFilter] units>, which themselves
|
||||
@ -269,33 +270,34 @@ fun String.tr(): String {
|
||||
* of the rest of the translatable string.
|
||||
* All of this nesting makes it quite difficult to translate, and is the reason we check
|
||||
* for these first.
|
||||
*
|
||||
*
|
||||
* The plan: First translate each of the conditionals on its own, and then combine them
|
||||
* together into the final fully translated string.
|
||||
*/
|
||||
|
||||
|
||||
var translatedBaseUnique = this.removeConditionals().tr()
|
||||
|
||||
|
||||
val conditionals = this.getConditionals().map { it.placeholderText }
|
||||
val conditionsWithTranslation: HashMap<String, String> = hashMapOf()
|
||||
|
||||
|
||||
for (conditional in this.getConditionals())
|
||||
conditionsWithTranslation[conditional.placeholderText] = conditional.text.tr()
|
||||
|
||||
|
||||
val translatedConditionals: MutableList<String> = mutableListOf()
|
||||
|
||||
|
||||
// Somewhere, we asked the translators to reorder all possible conditionals in a way that
|
||||
// makes sense in their language. We get this ordering, and than extract each of the
|
||||
// translated conditionals, removing the <> surrounding them, and removing param values
|
||||
// where it exists.
|
||||
val conditionalOrdering = UncivGame.Current.translations.getConditionalOrder(language)
|
||||
for (placedConditional in pointyBraceRegex.findAll(conditionalOrdering).map { it.value.substring(1, it.value.length-1).getPlaceholderText() }) {
|
||||
for (placedConditional in pointyBraceRegex.findAll(conditionalOrdering)
|
||||
.map { it.value.substring(1, it.value.length - 1).getPlaceholderText() }) {
|
||||
if (placedConditional in conditionals) {
|
||||
translatedConditionals.add(conditionsWithTranslation[placedConditional]!!)
|
||||
conditionsWithTranslation.remove(placedConditional)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// If the translated string that should contain all conditionals doesn't contain
|
||||
// a few conditionals used here, just add the translations of these to the end.
|
||||
// We do test for this, but just in case.
|
||||
@ -309,8 +311,10 @@ fun String.tr(): String {
|
||||
translatedBaseUnique = translatedBaseUnique.replaceFirstChar { it.lowercase() }
|
||||
translatedConditionals.add(translatedBaseUnique)
|
||||
}
|
||||
|
||||
var fullyTranslatedString = translatedConditionals.joinToString(UncivGame.Current.translations.getSpaceEquivalent(language))
|
||||
|
||||
var fullyTranslatedString = translatedConditionals.joinToString(
|
||||
UncivGame.Current.translations.getSpaceEquivalent(language)
|
||||
)
|
||||
if (UncivGame.Current.translations.shouldCapitalize(language))
|
||||
fullyTranslatedString = fullyTranslatedString.replaceFirstChar { it.uppercase() }
|
||||
return fullyTranslatedString
|
||||
@ -335,7 +339,7 @@ fun String.tr(): String {
|
||||
val translationStringWithSquareBracketsOnly = this.getPlaceholderText()
|
||||
// That is now the key into the translation HashMap!
|
||||
val translationEntry = UncivGame.Current.translations
|
||||
.get(translationStringWithSquareBracketsOnly, language, activeMods)
|
||||
.get(translationStringWithSquareBracketsOnly, language, activeMods)
|
||||
|
||||
var languageSpecificPlaceholder: String
|
||||
val originalEntry: String
|
||||
@ -351,12 +355,16 @@ fun String.tr(): String {
|
||||
// Take the terms in the message, WITHOUT square brackets
|
||||
val termsInMessage = this.getPlaceholderParameters()
|
||||
// Take the term from the placeholder, INCLUDING the square brackets
|
||||
val termsInTranslationPlaceholder = squareBraceRegex.findAll(originalEntry).map { it.value }.toList()
|
||||
val termsInTranslationPlaceholder =
|
||||
squareBraceRegex.findAll(originalEntry).map { it.value }.toList()
|
||||
if (termsInMessage.size != termsInTranslationPlaceholder.size)
|
||||
throw Exception("Message $this has a different number of terms than the placeholder $translationEntry!")
|
||||
|
||||
for (i in termsInMessage.indices) {
|
||||
languageSpecificPlaceholder = languageSpecificPlaceholder.replace(termsInTranslationPlaceholder[i], termsInMessage[i].tr())
|
||||
languageSpecificPlaceholder = languageSpecificPlaceholder.replace(
|
||||
termsInTranslationPlaceholder[i],
|
||||
termsInMessage[i].tr()
|
||||
)
|
||||
}
|
||||
return languageSpecificPlaceholder // every component is already translated
|
||||
}
|
||||
@ -367,7 +375,12 @@ fun String.tr(): String {
|
||||
|
||||
if (Stats.isStats(this)) return Stats.parse(this).toString()
|
||||
|
||||
return UncivGame.Current.translations.getText(this, language, activeMods)
|
||||
val translation = UncivGame.Current.translations.getText(this, language, activeMods)
|
||||
|
||||
val stat = Stat.values().firstOrNull { it.name == this }
|
||||
if (stat != null) return stat.character + translation
|
||||
|
||||
return translation
|
||||
}
|
||||
|
||||
fun String.getPlaceholderText() = this
|
||||
|
Reference in New Issue
Block a user