From ad50e9d2fc56cf76a1553fce7c51afc13573ff53 Mon Sep 17 00:00:00 2001 From: SimonCeder <63475501+SimonCeder@users.noreply.github.com> Date: Sun, 5 Sep 2021 11:12:18 +0200 Subject: [PATCH] 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 --- .../com/unciv/models/metadata/GameSettings.kt | 71 +++++++++++++++++++ core/src/com/unciv/models/ruleset/Belief.kt | 3 +- core/src/com/unciv/ui/LanguagePickerScreen.kt | 1 + .../unciv/ui/civilopedia/CivilopediaScreen.kt | 3 +- .../ui/newgamescreen/PlayerPickerTable.kt | 3 +- .../src/com/unciv/ui/trade/DiplomacyScreen.kt | 3 +- .../ui/worldscreen/mainmenu/OptionsPopup.kt | 1 + 7 files changed, 77 insertions(+), 8 deletions(-) diff --git a/core/src/com/unciv/models/metadata/GameSettings.kt b/core/src/com/unciv/models/metadata/GameSettings.kt index 3dffd8f1d1..154385dc60 100644 --- a/core/src/com/unciv/models/metadata/GameSettings.kt +++ b/core/src/com/unciv/models/metadata/GameSettings.kt @@ -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() var tutorialTasksCompleted = HashSet() @@ -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"), } diff --git a/core/src/com/unciv/models/ruleset/Belief.kt b/core/src/com/unciv/models/ruleset/Belief.kt index bea48c6ceb..c4ea7b0d30 100644 --- a/core/src/com/unciv/models/ruleset/Belief.kt +++ b/core/src/com/unciv/models/ruleset/Belief.kt @@ -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() 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 diff --git a/core/src/com/unciv/ui/LanguagePickerScreen.kt b/core/src/com/unciv/ui/LanguagePickerScreen.kt index 10ca77d2ec..bfef5e85b3 100644 --- a/core/src/com/unciv/ui/LanguagePickerScreen.kt +++ b/core/src/com/unciv/ui/LanguagePickerScreen.kt @@ -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() diff --git a/core/src/com/unciv/ui/civilopedia/CivilopediaScreen.kt b/core/src/com/unciv/ui/civilopedia/CivilopediaScreen.kt index 570c84b11f..66ce7022f3 100644 --- a/core/src/com/unciv/ui/civilopedia/CivilopediaScreen.kt +++ b/core/src/com/unciv/ui/civilopedia/CivilopediaScreen.kt @@ -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{ it.sortBy } - .thenBy (Collator.getInstance(), { it.name.tr() }) + .thenBy (UncivGame.Current.settings.getCollatorFromLocale(), { it.name.tr() }) ) var currentY = -1f diff --git a/core/src/com/unciv/ui/newgamescreen/PlayerPickerTable.kt b/core/src/com/unciv/ui/newgamescreen/PlayerPickerTable.kt index 85b3e096da..09f487ced2 100644 --- a/core/src/com/unciv/ui/newgamescreen/PlayerPickerTable.kt +++ b/core/src/com/unciv/ui/newgamescreen/PlayerPickerTable.kt @@ -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 diff --git a/core/src/com/unciv/ui/trade/DiplomacyScreen.kt b/core/src/com/unciv/ui/trade/DiplomacyScreen.kt index d7424cefd5..ace6a63d0d 100644 --- a/core/src/com/unciv/ui/trade/DiplomacyScreen.kt +++ b/core/src/com/unciv/ui/trade/DiplomacyScreen.kt @@ -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{ it.isMajorCiv() } - .thenBy (Collator.getInstance(), { it.civName.tr() }) + .thenBy (UncivGame.Current.settings.getCollatorFromLocale(), { it.civName.tr() }) ) for (civ in civsToDisplay) { diff --git a/core/src/com/unciv/ui/worldscreen/mainmenu/OptionsPopup.kt b/core/src/com/unciv/ui/worldscreen/mainmenu/OptionsPopup.kt index db943c29cb..e33d04bcc7 100644 --- a/core/src/com/unciv/ui/worldscreen/mainmenu/OptionsPopup.kt +++ b/core/src/com/unciv/ui/worldscreen/mainmenu/OptionsPopup.kt @@ -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() }