From d12d940e0f9ce7c9bf6da92a157b300b231208bd Mon Sep 17 00:00:00 2001 From: Peter Storch Date: Sun, 29 Oct 2023 19:40:54 +0100 Subject: [PATCH] refactor country name translation --- .../bahnhofsfotos/db/CountryAdapter.kt | 22 +++--- app/src/main/res/values/arrays.xml | 68 +++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + 3 files changed, 83 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/de/bahnhoefe/deutschlands/bahnhofsfotos/db/CountryAdapter.kt b/app/src/main/kotlin/de/bahnhoefe/deutschlands/bahnhofsfotos/db/CountryAdapter.kt index df2064e5..3d704a19 100644 --- a/app/src/main/kotlin/de/bahnhoefe/deutschlands/bahnhofsfotos/db/CountryAdapter.kt +++ b/app/src/main/kotlin/de/bahnhoefe/deutschlands/bahnhofsfotos/db/CountryAdapter.kt @@ -13,8 +13,8 @@ import de.bahnhoefe.deutschlands.bahnhofsfotos.databinding.ItemCountryBinding import de.bahnhoefe.deutschlands.bahnhofsfotos.util.Constants.COUNTRIES class CountryAdapter( - private val context: Context, - countryCodes: Set, + context: Context, + selectedCountryCodes: Set, cursor: Cursor, flags: Int, ) : CursorAdapter( @@ -22,7 +22,17 @@ class CountryAdapter( ) { private val layoutInflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater - val selectedCountries: MutableSet = countryCodes.toMutableSet() + val selectedCountries: MutableSet = selectedCountryCodes.toMutableSet() + private val countryNamesByCode: Map + + init { + val countryCodes = context.resources.getStringArray(R.array.country_codes) + val countryNames = context.resources.getStringArray(R.array.country_names) + countryNamesByCode = countryCodes.mapIndexed { index, code -> + Pair(code, countryNames[index]) + } + .toMap() + } companion object { private val TAG = CountryAdapter::class.java.simpleName @@ -63,11 +73,7 @@ class CountryAdapter( } private fun getCountryName(countryCode: String, defaultName: String): String { - val strId = - context.resources.getIdentifier("country_$countryCode", "string", context.packageName) - return if (strId != 0) { - context.getString(strId) - } else defaultName + return countryNamesByCode[countryCode] ?: defaultName } override fun newView(context: Context, cursor: Cursor, parent: ViewGroup): View { diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 26e32a4d..4ce3da6d 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -11,4 +11,72 @@ @string/active_flag_inactive + + al + at + au + ba + be + ca + ch + cn + cz + dk + de + ee + es + fi + fr + hr + hu + ie + in + jp + lt + md + nl + no + pl + pt + ru + sk + tw + uk + us + + + + @string/country_al + @string/country_at + @string/country_au + @string/country_ba + @string/country_be + @string/country_ca + @string/country_ch + @string/country_cn + @string/country_cz + @string/country_dk + @string/country_de + @string/country_ee + @string/country_es + @string/country_fi + @string/country_fr + @string/country_hr + @string/country_hu + @string/country_ie + @string/country_in + @string/country_jp + @string/country_lt + @string/country_md + @string/country_nl + @string/country_no + @string/country_pl + @string/country_pt + @string/country_ru + @string/country_sk + @string/country_tw + @string/country_uk + @string/country_us + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e1dd5ace..db463933 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -286,6 +286,7 @@ Albania Austria Australia + Bosnia and Herzegovina Belgium Canada Switzerland