From 3943358db3a9485151abc8ca08e61627213f92c5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 15 Sep 2018 18:02:48 +0800 Subject: [PATCH] improve resolve of language --- .../CeilDescriptions/CeilInfoResolver.kt | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/utils/CeilDescriptions/CeilInfoResolver.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/utils/CeilDescriptions/CeilInfoResolver.kt index 68c46fe..0634a1a 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/utils/CeilDescriptions/CeilInfoResolver.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/utils/CeilDescriptions/CeilInfoResolver.kt @@ -10,8 +10,25 @@ const val characteristicsFolder = "characteristics" private val gson: Gson = GsonBuilder().create() +val availableTranslations = listOf( + "en_US" +) + +private val defaultLanguage = availableTranslations.first() + +private fun findSubTranslation(language: String): String { + return if (availableTranslations.contains(language)) { + return language + } else { + val localeUpper = language.split("_").first() + availableTranslations.firstOrNull { + it.startsWith(localeUpper) + } ?: defaultLanguage + } +} + fun resolveCeilsDescriptionsByLanguage(language: String = "en_US"): CeilsInfosRoot { - return load("$characteristicsFolder/$language.json").let { + return load("$characteristicsFolder/${findSubTranslation(language)}.json").let { gson.fromJson(InputStreamReader(it), CeilsInfosRoot::class.java) } }