Skip to content

Commit

Permalink
Add support for both uppercase and lowercase language Strings
Browse files Browse the repository at this point in the history
  • Loading branch information
janseeger committed Feb 22, 2024
1 parent a67f49f commit f99c8c0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ sealed interface UiText {

fun Configuration.getStringForLocales(): String? {
val locale = resolveLocale(language.keys)
return language[locale?.language?.uppercase()]
return language[locale?.language?.lowercase()] ?: language[locale?.language?.uppercase()]
}

private fun Configuration.resolveLocale(supportedLanguages: Set<String>): Locale? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,41 @@ class UiTextTest {
expectResolvedComposeString("Annehmen", uiText)
}

private val languageMap = mapOf(
private val upperCaselanguageMap = mapOf(
"EN" to "String",
"DE" to "Zeichenfolge"
)

@Test
@Config(qualifiers = "de")
fun multiLangStringResolvesValueWhenRequestedDe() {
val uiText = UiText.MultiLangString(languageMap)
fun multiLangStringResolvesValueWhenRequestedUpperCaseDe() {
val uiText = UiText.MultiLangString(upperCaselanguageMap)
expectResolvedResourceString("Zeichenfolge", uiText)
}

@Test
@Config(qualifiers = "en")
fun multiLangStringResolvesValueWhenRequestedEn() {
val uiText = UiText.MultiLangString(languageMap)
fun multiLangStringResolvesValueWhenRequestedUpperCaseEn() {
val uiText = UiText.MultiLangString(upperCaselanguageMap)
expectResolvedResourceString("String", uiText)
}

private val lowerCaselanguageMap = mapOf(
"en" to "String",
"de" to "Zeichenfolge"
)

@Test
@Config(qualifiers = "de")
fun multiLangStringResolvesValueWhenRequestedLowerCaseDe() {
val uiText = UiText.MultiLangString(lowerCaselanguageMap)
expectResolvedResourceString("Zeichenfolge", uiText)
}

@Test
@Config(qualifiers = "en")
fun multiLangStringResolvesValueWhenRequestedLowerCaseEn() {
val uiText = UiText.MultiLangString(lowerCaselanguageMap)
expectResolvedResourceString("String", uiText)
}

Expand Down Expand Up @@ -122,16 +141,6 @@ class UiTextTest {
expectResolvedComposeString("Answer", uiText)
}

@Test
@Config(qualifiers = "en")
fun multiLangKeyInWrongCaseWill() {
val uiText = UiText.MultiLangString(
mapOf("en" to "some string"),
fallbackResource = R.string.call_notification_answer_action
)
expectResolvedComposeString("Answer", uiText)
}

private fun expectResolvedResourceString(expected: String, uiText: UiText) {
assertEquals(expected, uiText.asString(context.resources))
}
Expand Down

0 comments on commit f99c8c0

Please sign in to comment.