mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-24 03:53:12 -04:00
Split tr() into subfunctions, was getting very large for a single function
This commit is contained in:
parent
2ef7c15538
commit
dcaf3bd68e
@ -310,7 +310,29 @@ fun String.tr(hideIcons: Boolean = false): String {
|
|||||||
val language: String = UncivGame.Current.settings.language
|
val language: String = UncivGame.Current.settings.language
|
||||||
|
|
||||||
// '<' and '>' checks for quick 'no' answer, regex to ensure that no one accidentally put '><' and ruined things
|
// '<' and '>' checks for quick 'no' answer, regex to ensure that no one accidentally put '><' and ruined things
|
||||||
if (contains('<') && contains('>') && pointyBraceRegex.containsMatchIn(this)) { // Conditionals!
|
if (contains('<') && contains('>') && pointyBraceRegex.containsMatchIn(this)) {
|
||||||
|
return translateConditionals(hideIcons, language)
|
||||||
|
}
|
||||||
|
|
||||||
|
// curly and square brackets can be nested inside of each other so find the leftmost curly/square
|
||||||
|
// bracket then process that first
|
||||||
|
val indexSquare = this.indexOf('[')
|
||||||
|
val indexCurly = this.indexOf('{')
|
||||||
|
|
||||||
|
val squareBracketsEncounteredFirst = indexSquare >= 0 && (indexCurly < 0 || indexSquare < indexCurly)
|
||||||
|
val curlyBracketsEncounteredFirst = indexCurly >= 0 && (indexSquare < 0 || indexCurly < indexSquare)
|
||||||
|
|
||||||
|
if (squareBracketsEncounteredFirst)
|
||||||
|
return translatePlaceholders(language, hideIcons)
|
||||||
|
|
||||||
|
if (curlyBracketsEncounteredFirst) // Translating partial sentences
|
||||||
|
return curlyBraceRegex.replace(this) { it.groups[1]!!.value.tr(hideIcons) }
|
||||||
|
|
||||||
|
return translateIndividualWord(language, hideIcons)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun String.translateConditionals(hideIcons: Boolean, language: String): String {
|
||||||
/**
|
/**
|
||||||
* So conditionals can contain placeholders, such as <vs [unitFilter] units>, which themselves
|
* So conditionals can contain placeholders, such as <vs [unitFilter] units>, which themselves
|
||||||
* can contain multiple filters, such as <vs [{Military} {Water}] units>.
|
* can contain multiple filters, such as <vs [{Military} {Water}] units>.
|
||||||
@ -369,15 +391,7 @@ fun String.tr(hideIcons: Boolean = false): String {
|
|||||||
return fullyTranslatedString
|
return fullyTranslatedString
|
||||||
}
|
}
|
||||||
|
|
||||||
// curly and square brackets can be nested inside of each other so find the leftmost curly/square
|
private fun String.translatePlaceholders(language: String, hideIcons: Boolean): String {
|
||||||
// bracket then process that first
|
|
||||||
val indexSquare = this.indexOf('[')
|
|
||||||
val indexCurly = this.indexOf('{')
|
|
||||||
val processSquare = indexSquare >= 0 && (indexCurly < 0 || indexSquare < indexCurly)
|
|
||||||
val processCurly = indexCurly >= 0 && (indexSquare < 0 || indexCurly < indexSquare)
|
|
||||||
|
|
||||||
// There might still be optimization potential here!
|
|
||||||
if (processSquare) { // Placeholders!
|
|
||||||
/**
|
/**
|
||||||
* I'm SURE there's an easier way to do this but I can't think of it =\
|
* I'm SURE there's an easier way to do this but I can't think of it =\
|
||||||
* So what's all this then?
|
* So what's all this then?
|
||||||
@ -424,10 +438,9 @@ fun String.tr(hideIcons: Boolean = false): String {
|
|||||||
return languageSpecificPlaceholder // every component is already translated
|
return languageSpecificPlaceholder // every component is already translated
|
||||||
}
|
}
|
||||||
|
|
||||||
if (processCurly) { // Translating partial sentences
|
|
||||||
return curlyBraceRegex.replace(this) { it.groups[1]!!.value.tr(hideIcons) }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/** No brackets of any kind, just a single word */
|
||||||
|
private fun String.translateIndividualWord(language: String, hideIcons: Boolean): String {
|
||||||
if (Stats.isStats(this)) return Stats.parse(this).toString()
|
if (Stats.isStats(this)) return Stats.parse(this).toString()
|
||||||
|
|
||||||
val translation = UncivGame.Current.translations.getText(this, language, TranslationActiveModsCache.activeMods)
|
val translation = UncivGame.Current.translations.getText(this, language, TranslationActiveModsCache.activeMods)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user