Skip to content

Commit

Permalink
Enforce upper case language keys for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
janseeger committed Dec 20, 2023
1 parent e505750 commit 8c2b770
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.toUpperCase

sealed interface UiText {
data class DynamicString(val value: String) : UiText
Expand All @@ -32,7 +33,7 @@ sealed interface UiText {

fun LocaleList.getStringForLocales(): String? {
val locale = getFirstMatch(language.keys.toTypedArray())
return language[locale?.language]
return language[locale?.language?.uppercase()]
}
}

Expand Down
49 changes: 43 additions & 6 deletions dachlatten-compose/src/test/kotlin/text/UiTextTest.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package de.sipgate.dachlatten.compose.text

import android.content.res.Resources
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.core.R
import org.junit.Rule
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.assertThrows
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
Expand Down Expand Up @@ -47,8 +49,8 @@ class UiTextTest {
}

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

@Test
Expand All @@ -68,7 +70,7 @@ class UiTextTest {
@Test
@Config(qualifiers = "en")
fun stringSubstitutionWorks() {
val uiText = UiText.MultiLangString(mapOf("en" to "string %s"),
val uiText = UiText.MultiLangString(mapOf("EN" to "string %s"),
fallbackResource = null,
"substitution")
expectResolvedResourceString("string substitution", uiText)
Expand All @@ -77,7 +79,7 @@ class UiTextTest {
@Test
@Config(qualifiers = "en")
fun stringSubstitutionWorksForMultipleArguments() {
val uiText = UiText.MultiLangString(mapOf("en" to "string %s and %s"),
val uiText = UiText.MultiLangString(mapOf("EN" to "string %s and %s"),
fallbackResource = null,
"substitution", "others")
expectResolvedResourceString("string substitution and others", uiText)
Expand All @@ -86,20 +88,55 @@ class UiTextTest {
@Test
@Config(qualifiers = "en")
fun stringSubstitutionWorksFromCompose() {
val uiText = UiText.MultiLangString(mapOf("en" to "string %s and %s"),
val uiText = UiText.MultiLangString(mapOf("EN" to "string %s and %s"),
fallbackResource = null,
"substitution", "others")
expectResolvedComposeString("string substitution and others", uiText)
}

@Test
@Config(qualifiers = "en")
fun exceptionIsThrownWhenTheTranslationCannotBeFound() {
val uiText = UiText.MultiLangString(mapOf("invalid-key" to "some string"))

assertThrows<Resources.NotFoundException> {
composeTestRule.setContent {
uiText.asString()
}
}
}

@Test
@Config(qualifiers = "en")
fun fallbackIsUsedIfTranslationCannotBeFound() {
val uiText = UiText.MultiLangString(mapOf("invalid-key" to "some string"),
fallbackResource = R.string.call_notification_answer_action)
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))
}

private fun expectResolvedComposeString(expected: String, uiText: UiText) {
composeTestRule.setContent {
val resolvedString = uiText.asString()
assertEquals(expected, resolvedString)

/*
* This is needed because the Strings we have packaged with Robolectric
* returns BiDi marks, which causes the String comparison to fail.
*/
val sanitizedString = resolvedString.filter { it.isLetterOrDigit() || it.isWhitespace() }

assertEquals(expected, sanitizedString)
}
}
}

0 comments on commit 8c2b770

Please sign in to comment.