From b3cd36c70e6b237ac88d04dce56c7036a02f84ae Mon Sep 17 00:00:00 2001 From: OptimizedForDensity <105244635+OptimizedForDensity@users.noreply.github.com> Date: Fri, 1 Jul 2022 02:32:56 -0400 Subject: [PATCH] Fix translation issues caused by nested brackets and braces (#7242) * Fix translation issues * Readd comment * Reviews * Update translation line --- .../assets/jsons/translations/template.properties | 3 +-- .../com/unciv/models/translations/Translations.kt | 13 +++++++++---- .../ui/utils/extensions/FormattingExtensions.kt | 5 ++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/android/assets/jsons/translations/template.properties b/android/assets/jsons/translations/template.properties index 9ab59d8103..f1cde8bdc6 100644 --- a/android/assets/jsons/translations/template.properties +++ b/android/assets/jsons/translations/template.properties @@ -86,7 +86,6 @@ Requires a [buildingName] in all cities = [buildingName] required: = Requires a [buildingName] in this city = Cannot be built with [buildingName] = -Consumes 1 [resource] = Consumes [amount] [resource] = [amount] available = Required tech: [requiredTech] = @@ -1186,7 +1185,7 @@ Number of your cities\ndemanding this resource for\n'We Love The King Day' = [victoryType] Victory = Built [building] = -Add all spaceship parts in capital = +Add all [comment] in capital = Destroy all players = Capture all capitals = Complete [amount] Policy branches = diff --git a/core/src/com/unciv/models/translations/Translations.kt b/core/src/com/unciv/models/translations/Translations.kt index fc7da3dd01..86348cdf3a 100644 --- a/core/src/com/unciv/models/translations/Translations.kt +++ b/core/src/com/unciv/models/translations/Translations.kt @@ -350,8 +350,15 @@ fun String.tr(): 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 (contains('[')) { // Placeholders! + if (processSquare) { // Placeholders! /** * I'm SURE there's an easier way to do this but I can't think of it =\ * So what's all this then? @@ -399,12 +406,10 @@ fun String.tr(): String { return languageSpecificPlaceholder // every component is already translated } - - if (contains('{')) { // Translating partial sentences + if (processCurly) { // Translating partial sentences return curlyBraceRegex.replace(this) { it.groups[1]!!.value.tr() } } - if (Stats.isStats(this)) return Stats.parse(this).toString() val translation = UncivGame.Current.translations.getText(this, language, TranslationActiveModsCache.activeMods) diff --git a/core/src/com/unciv/ui/utils/extensions/FormattingExtensions.kt b/core/src/com/unciv/ui/utils/extensions/FormattingExtensions.kt index 48ec45a954..dc7e3fdfc5 100644 --- a/core/src/com/unciv/ui/utils/extensions/FormattingExtensions.kt +++ b/core/src/com/unciv/ui/utils/extensions/FormattingExtensions.kt @@ -15,9 +15,8 @@ fun Int.toPercent() = toFloat().toPercent() /** Translate a percentage number - e.g. 25 - to the multiplication value - e.g. 1.25f */ fun Float.toPercent() = 1 + this/100 -/** Convert a [resource name][this] into "Consumes [amount] $resource" string (untranslated, using separate templates for 1 and other amounts) */ -//todo some day... remove and use just one translatable where this is called -fun String.getConsumesAmountString(amount: Int) = if (amount == 1) "Consumes 1 [$this]" else "Consumes [$amount] [$this]" +/** Convert a [resource name][this] into "Consumes [amount] $resource" string (untranslated) */ +fun String.getConsumesAmountString(amount: Int) = "Consumes [$amount] [$this]" /** Formats the [Duration] into a translated string */ fun Duration.format(): String {