Skip to content

Commit

Permalink
refactor country name translation
Browse files Browse the repository at this point in the history
  • Loading branch information
pstorch committed Oct 29, 2023
1 parent 0610160 commit d12d940
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,26 @@ import de.bahnhoefe.deutschlands.bahnhofsfotos.databinding.ItemCountryBinding
import de.bahnhoefe.deutschlands.bahnhofsfotos.util.Constants.COUNTRIES

class CountryAdapter(
private val context: Context,
countryCodes: Set<String>,
context: Context,
selectedCountryCodes: Set<String>,
cursor: Cursor,
flags: Int,
) : CursorAdapter(
context, cursor, flags
) {
private val layoutInflater: LayoutInflater =
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val selectedCountries: MutableSet<String> = countryCodes.toMutableSet()
val selectedCountries: MutableSet<String> = selectedCountryCodes.toMutableSet()
private val countryNamesByCode: Map<String, String>

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
Expand Down Expand Up @@ -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 {
Expand Down
68 changes: 68 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,72 @@
<item>@string/active_flag_inactive</item>
</string-array>

<string-array name="country_codes">
<item>al</item>
<item>at</item>
<item>au</item>
<item>ba</item>
<item>be</item>
<item>ca</item>
<item>ch</item>
<item>cn</item>
<item>cz</item>
<item>dk</item>
<item>de</item>
<item>ee</item>
<item>es</item>
<item>fi</item>
<item>fr</item>
<item>hr</item>
<item>hu</item>
<item>ie</item>
<item>in</item>
<item>jp</item>
<item>lt</item>
<item>md</item>
<item>nl</item>
<item>no</item>
<item>pl</item>
<item>pt</item>
<item>ru</item>
<item>sk</item>
<item>tw</item>
<item>uk</item>
<item>us</item>
</string-array>

<string-array name="country_names">
<item>@string/country_al</item>
<item>@string/country_at</item>
<item>@string/country_au</item>
<item>@string/country_ba</item>
<item>@string/country_be</item>
<item>@string/country_ca</item>
<item>@string/country_ch</item>
<item>@string/country_cn</item>
<item>@string/country_cz</item>
<item>@string/country_dk</item>
<item>@string/country_de</item>
<item>@string/country_ee</item>
<item>@string/country_es</item>
<item>@string/country_fi</item>
<item>@string/country_fr</item>
<item>@string/country_hr</item>
<item>@string/country_hu</item>
<item>@string/country_ie</item>
<item>@string/country_in</item>
<item>@string/country_jp</item>
<item>@string/country_lt</item>
<item>@string/country_md</item>
<item>@string/country_nl</item>
<item>@string/country_no</item>
<item>@string/country_pl</item>
<item>@string/country_pt</item>
<item>@string/country_ru</item>
<item>@string/country_sk</item>
<item>@string/country_tw</item>
<item>@string/country_uk</item>
<item>@string/country_us</item>
</string-array>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@
<string name="country_al">Albania</string>
<string name="country_at">Austria</string>
<string name="country_au">Australia</string>
<string name="country_ba">Bosnia and Herzegovina</string>
<string name="country_be">Belgium</string>
<string name="country_ca">Canada</string>
<string name="country_ch">Switzerland</string>
Expand Down

0 comments on commit d12d940

Please sign in to comment.