mirror of
https://github.com/yairm210/Unciv.git
synced 2025-08-02 16:19:41 +07:00
Split tr() into subfunctions, was getting very large for a single function
This commit is contained in:
@ -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)
|
||||
|
Reference in New Issue
Block a user