Translate nested placeholders for english too (#4319)

This commit is contained in:
SomeTroglodyte
2021-06-30 19:40:41 +02:00
committed by GitHub
parent 3b75f2209e
commit 44674365fe

View File

@ -231,7 +231,7 @@ fun String.tr(): String {
}
// 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 =\
* 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 []"
val translationStringWithSquareBracketsOnly = this.getPlaceholderText()
val language = UncivGame.Current.settings.language
// That is now the key into the translation HashMap!
val translationEntry = UncivGame.Current.translations
.get(translationStringWithSquareBracketsOnly, UncivGame.Current.settings.language, activeMods)
.get(translationStringWithSquareBracketsOnly, language, activeMods)
if (translationEntry == null ||
!translationEntry.containsKey(UncivGame.Current.settings.language)) {
var languageSpecificPlaceholder: String
val originalEntry: String
if (translationEntry == null || !translationEntry.containsKey(language)) {
// 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
val termsInMessage = this.getPlaceholderParameters()
// 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)
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) {
languageSpecificPlaceholder = languageSpecificPlaceholder.replace(termsInTranslationPlaceholder[i], termsInMessage[i].tr())
}
return languageSpecificPlaceholder // every component is already translated
}
if (contains("{")) { // sentence
if (contains('{')) { // sentence
return curlyBraceRegex.replace(this) { it.groups[1]!!.value.tr() }
}