Split tr() into subfunctions, was getting very large for a single function

This commit is contained in:
yairm210
2024-07-01 09:21:48 +03:00
parent 2ef7c15538
commit dcaf3bd68e

View File

@ -310,7 +310,29 @@ fun String.tr(hideIcons: Boolean = false): String {
val language: String = UncivGame.Current.settings.language
// '<' 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
* can contain multiple filters, such as <vs [{Military} {Water}] units>.
@ -369,15 +391,7 @@ fun String.tr(hideIcons: Boolean = false): String {
return fullyTranslatedString
}
// 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 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!
private fun String.translatePlaceholders(language: String, hideIcons: Boolean): String {
/**
* I'm SURE there's an easier way to do this but I can't think of it =\
* So what's all this then?
@ -424,10 +438,9 @@ fun String.tr(hideIcons: Boolean = false): String {
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()
val translation = UncivGame.Current.translations.getText(this, language, TranslationActiveModsCache.activeMods)