mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-27 13:55:54 -04:00
implement locale for proper sorting on certain screens (#5082)
* implement locale for sorting * persian, centralize Collator.getInstance * fall back to default instead of english * fix failing build
This commit is contained in:
parent
a20baca7c2
commit
ad50e9d2fc
@ -2,7 +2,11 @@ package com.unciv.models.metadata
|
||||
|
||||
import com.badlogic.gdx.Application
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.unciv.UncivGame
|
||||
import com.unciv.logic.GameSaver
|
||||
import java.text.Collator
|
||||
import java.util.*
|
||||
import kotlin.collections.HashSet
|
||||
|
||||
data class WindowState (val width: Int = 900, val height: Int = 600)
|
||||
|
||||
@ -13,6 +17,8 @@ class GameSettings {
|
||||
var checkForDueUnits: Boolean = true
|
||||
var singleTapMove: Boolean = false
|
||||
var language: String = "English"
|
||||
@Transient
|
||||
var locale: Locale? = null
|
||||
var resolution: String = "900x600" // Auto-detecting resolution was a BAD IDEA since it needs to be based on DPI AND resolution.
|
||||
var tutorialsShown = HashSet<String>()
|
||||
var tutorialTasksCompleted = HashSet<String>()
|
||||
@ -68,4 +74,69 @@ class GameSettings {
|
||||
if (tutorialTasksCompleted.add(tutorialTask))
|
||||
save()
|
||||
}
|
||||
|
||||
fun updateLocaleFromLanguage() {
|
||||
val bannedCharacters = listOf(' ', '_', '-', '(', ')') // Things not to have in enum names
|
||||
val languageName = language.filterNot { it in bannedCharacters }
|
||||
try {
|
||||
val code = LocaleCode.valueOf(languageName)
|
||||
locale = Locale(code.language, code.country)
|
||||
} catch (e: Exception) {
|
||||
locale = Locale.getDefault()
|
||||
}
|
||||
}
|
||||
|
||||
fun getCurrentLocale(): Locale {
|
||||
if (locale == null)
|
||||
updateLocaleFromLanguage()
|
||||
return locale!!
|
||||
}
|
||||
|
||||
fun getCollatorFromLocale(): Collator {
|
||||
return Collator.getInstance(getCurrentLocale())
|
||||
}
|
||||
}
|
||||
|
||||
enum class LocaleCode(var language: String, var country: String) {
|
||||
Arabic("ar", "IQ"),
|
||||
BrazilianPortuguese("pt", "BR"),
|
||||
Bulgarian("bg", "BG"),
|
||||
Catalan("ca", "ES"),
|
||||
Croatian("hr", "HR"),
|
||||
Czech("cs", "CZ"),
|
||||
Danish("da", "DK"),
|
||||
Dutch("nl", "NL"),
|
||||
English("en", "US"),
|
||||
Estonian("et", "EE"),
|
||||
Finnish("fi", "FI"),
|
||||
French("fr", "FR"),
|
||||
German("de", "DE"),
|
||||
Greek("el", "GR"),
|
||||
Hindi("hi", "IN"),
|
||||
Hungarian("hu", "HU"),
|
||||
Indonesian("in", "ID"),
|
||||
Italian("it", "IT"),
|
||||
Japanese("ja", "JP"),
|
||||
Korean("ko", "KR"),
|
||||
Latvian("lv", "LV"),
|
||||
Lithuanian("lt", "LT"),
|
||||
Malay("ms", "MY"),
|
||||
Norwegian("no", "NO"),
|
||||
NorwegianNynorsk("nn", "NO"),
|
||||
PersianPinglishDIN("fa", "IR"), // These might just fall back to default
|
||||
PersianPinglishUN("fa", "IR"),
|
||||
Polish("pl", "PL"),
|
||||
Portuguese("pt", "PT"),
|
||||
Romanian("ro", "RO"),
|
||||
Russian("ru", "RU"),
|
||||
Serbian("sr", "RS"),
|
||||
SimplifiedChinese("zh", "CN"),
|
||||
Slovak("sk", "SK"),
|
||||
Spanish("es", "ES"),
|
||||
Swedish("sv", "SE"),
|
||||
Thai("th", "TH"),
|
||||
TraditionalChinese("zh", "TW"),
|
||||
Turkish("tr", "TR"),
|
||||
Ukrainian("uk", "UA"),
|
||||
Vietnamese("vi", "VN"),
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import com.unciv.models.stats.INamed
|
||||
import com.unciv.models.translations.tr
|
||||
import com.unciv.ui.civilopedia.FormattedLine
|
||||
import com.unciv.ui.civilopedia.ICivilopediaText
|
||||
import java.text.Collator
|
||||
import java.util.ArrayList
|
||||
|
||||
class Belief : INamed, ICivilopediaText, IHasUniques {
|
||||
@ -62,7 +61,7 @@ class Belief : INamed, ICivilopediaText, IHasUniques {
|
||||
name = "Religions"
|
||||
val lines = ArrayList<FormattedLine>()
|
||||
lines += FormattedLine(separator = true)
|
||||
ruleset.religions.sortedWith(compareBy(Collator.getInstance(), { it.tr() })).forEach {
|
||||
ruleset.religions.sortedWith(compareBy(UncivGame.Current.settings.getCollatorFromLocale(), { it.tr() })).forEach {
|
||||
lines += FormattedLine(it, icon = "Belief/$it")
|
||||
}
|
||||
civilopediaText = lines
|
||||
|
@ -43,6 +43,7 @@ class LanguagePickerScreen : PickerScreen() {
|
||||
|
||||
fun pickLanguage(){
|
||||
game.settings.language = chosenLanguage
|
||||
game.settings.updateLocaleFromLanguage()
|
||||
game.settings.isFreshlyCreated = false // mark so the picker isn't called next launch
|
||||
game.settings.save()
|
||||
|
||||
|
@ -13,7 +13,6 @@ import com.unciv.models.ruleset.VictoryType
|
||||
import com.unciv.models.stats.INamed
|
||||
import com.unciv.models.translations.tr
|
||||
import com.unciv.ui.utils.*
|
||||
import java.text.Collator
|
||||
import com.unciv.ui.utils.AutoScrollPane as ScrollPane
|
||||
|
||||
/** Screen displaying the Civilopedia
|
||||
@ -104,7 +103,7 @@ class CivilopediaScreen(
|
||||
// Alphabetical order of localized names, using system default locale
|
||||
entries = entries.sortedWith(
|
||||
compareBy<CivilopediaEntry>{ it.sortBy }
|
||||
.thenBy (Collator.getInstance(), { it.name.tr() })
|
||||
.thenBy (UncivGame.Current.settings.getCollatorFromLocale(), { it.name.tr() })
|
||||
)
|
||||
|
||||
var currentY = -1f
|
||||
|
@ -20,7 +20,6 @@ import com.unciv.models.translations.tr
|
||||
import com.unciv.ui.mapeditor.GameParametersScreen
|
||||
import com.unciv.ui.pickerscreens.PickerScreen
|
||||
import com.unciv.ui.utils.*
|
||||
import java.text.Collator
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
@ -273,7 +272,7 @@ private class NationPickerPopup(
|
||||
if (spectator != null) nations += spectator
|
||||
|
||||
nations += playerPicker.getAvailablePlayerCivs(player.chosenCiv)
|
||||
.sortedWith(compareBy(Collator.getInstance(), { it.name.tr() }))
|
||||
.sortedWith(compareBy(UncivGame.Current.settings.getCollatorFromLocale(), { it.name.tr() }))
|
||||
|
||||
var nationListScrollY = 0f
|
||||
var currentY = 0f
|
||||
|
@ -25,7 +25,6 @@ import com.unciv.ui.civilopedia.CivilopediaScreen
|
||||
import com.unciv.ui.tilegroups.CityButton
|
||||
import com.unciv.ui.utils.*
|
||||
import com.unciv.ui.utils.UncivTooltip.Companion.addTooltip
|
||||
import java.text.Collator
|
||||
import kotlin.math.floor
|
||||
import kotlin.math.roundToInt
|
||||
import com.unciv.ui.utils.AutoScrollPane as ScrollPane
|
||||
@ -70,7 +69,7 @@ class DiplomacyScreen(val viewingCiv:CivilizationInfo):CameraStageBaseScreen() {
|
||||
}
|
||||
.sortedWith(
|
||||
compareByDescending<CivilizationInfo>{ it.isMajorCiv() }
|
||||
.thenBy (Collator.getInstance(), { it.civName.tr() })
|
||||
.thenBy (UncivGame.Current.settings.getCollatorFromLocale(), { it.civName.tr() })
|
||||
)
|
||||
|
||||
for (civ in civsToDisplay) {
|
||||
|
@ -133,6 +133,7 @@ class OptionsPopup(val previousScreen: CameraStageBaseScreen) : Popup(previousSc
|
||||
var chosenLanguage = settings.language
|
||||
fun selectLanguage() {
|
||||
settings.language = chosenLanguage
|
||||
settings.updateLocaleFromLanguage()
|
||||
previousScreen.game.translations.tryReadTranslationForCurrentLanguage()
|
||||
reloadWorldAndOptions()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user