mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-28 14:24:43 -04:00
Translate nested placeholders for english too (#4319)
This commit is contained in:
parent
3b75f2209e
commit
44674365fe
@ -231,7 +231,7 @@ fun String.tr(): String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// There might still be optimization potential here!
|
// There might still be optimization potential here!
|
||||||
if (contains("[")) { // Placeholders!
|
if (contains('[')) { // 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?
|
||||||
@ -247,31 +247,36 @@ fun String.tr(): String {
|
|||||||
|
|
||||||
// Convert "work on [building] has completed in [city]" to "work on [] has completed in []"
|
// Convert "work on [building] has completed in [city]" to "work on [] has completed in []"
|
||||||
val translationStringWithSquareBracketsOnly = this.getPlaceholderText()
|
val translationStringWithSquareBracketsOnly = this.getPlaceholderText()
|
||||||
|
val language = UncivGame.Current.settings.language
|
||||||
// That is now the key into the translation HashMap!
|
// That is now the key into the translation HashMap!
|
||||||
val translationEntry = UncivGame.Current.translations
|
val translationEntry = UncivGame.Current.translations
|
||||||
.get(translationStringWithSquareBracketsOnly, UncivGame.Current.settings.language, activeMods)
|
.get(translationStringWithSquareBracketsOnly, language, activeMods)
|
||||||
|
|
||||||
if (translationEntry == null ||
|
var languageSpecificPlaceholder: String
|
||||||
!translationEntry.containsKey(UncivGame.Current.settings.language)) {
|
val originalEntry: String
|
||||||
|
if (translationEntry == null || !translationEntry.containsKey(language)) {
|
||||||
// Translation placeholder doesn't exist for this language, default to English
|
// Translation placeholder doesn't exist for this language, default to English
|
||||||
return this.replace(eitherSquareBraceRegex, "")
|
languageSpecificPlaceholder = this
|
||||||
|
originalEntry = this
|
||||||
|
} else {
|
||||||
|
languageSpecificPlaceholder = translationEntry[language]!!
|
||||||
|
originalEntry = translationEntry.entry
|
||||||
}
|
}
|
||||||
|
|
||||||
// Take the terms in the message, WITHOUT square brackets
|
// Take the terms in the message, WITHOUT square brackets
|
||||||
val termsInMessage = this.getPlaceholderParameters()
|
val termsInMessage = this.getPlaceholderParameters()
|
||||||
// Take the term from the placeholder, INCLUDING the square brackets
|
// Take the term from the placeholder, INCLUDING the square brackets
|
||||||
val termsInTranslationPlaceholder = squareBraceRegex.findAll(translationEntry.entry).map { it.value }.toList()
|
val termsInTranslationPlaceholder = squareBraceRegex.findAll(originalEntry).map { it.value }.toList()
|
||||||
if (termsInMessage.size != termsInTranslationPlaceholder.size)
|
if (termsInMessage.size != termsInTranslationPlaceholder.size)
|
||||||
throw Exception("Message $this has a different number of terms than the placeholder $translationEntry!")
|
throw Exception("Message $this has a different number of terms than the placeholder $translationEntry!")
|
||||||
|
|
||||||
var languageSpecificPlaceholder = translationEntry[UncivGame.Current.settings.language]!!
|
|
||||||
for (i in termsInMessage.indices) {
|
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
|
return languageSpecificPlaceholder // every component is already translated
|
||||||
}
|
}
|
||||||
|
|
||||||
if (contains("{")) { // sentence
|
if (contains('{')) { // sentence
|
||||||
return curlyBraceRegex.replace(this) { it.groups[1]!!.value.tr() }
|
return curlyBraceRegex.replace(this) { it.groups[1]!!.value.tr() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user