diff --git a/.idea/gradle.xml b/.idea/gradle.xml
index 68a7b59..8e6658a 100644
--- a/.idea/gradle.xml
+++ b/.idea/gradle.xml
@@ -31,6 +31,7 @@
+
diff --git a/dachlatten-android/build.gradle.kts b/dachlatten-android/build.gradle.kts
index 447b8e1..6b5c7cf 100644
--- a/dachlatten-android/build.gradle.kts
+++ b/dachlatten-android/build.gradle.kts
@@ -6,12 +6,14 @@ plugins {
}
dependencies {
+ compileOnly(project(":dachlatten-text"))
compileOnly(libs.androidx.activity)
implementation(libs.androidx.annotation)
implementation(libs.androidx.lifecycle.process)
implementation(libs.kotlinx.coroutines)
+ testImplementation(project(":dachlatten-text"))
testImplementation(libs.androidx.activity)
testImplementation(libs.androidx.core)
}
diff --git a/dachlatten-android/src/main/kotlin/de/sipgate/dachlatten/android/text/UiText.kt b/dachlatten-android/src/main/kotlin/de/sipgate/dachlatten/android/text/UiText.kt
deleted file mode 100644
index 667012c..0000000
--- a/dachlatten-android/src/main/kotlin/de/sipgate/dachlatten/android/text/UiText.kt
+++ /dev/null
@@ -1,57 +0,0 @@
-package de.sipgate.dachlatten.android.text
-
-import android.content.res.Configuration
-import android.content.res.Resources
-import android.os.Build
-import androidx.annotation.StringRes
-import java.util.Locale
-
-sealed interface UiText {
- data class DynamicString(val value: String) : UiText
-
- data class StringResource(
- @StringRes val resId: Int,
- val args: List,
- ) : UiText {
- constructor(
- @StringRes resId: Int,
- vararg args: Any) : this(resId, args.asList())
- }
-
- data class MultiLangString(
- private val language: Map,
- @StringRes val fallbackResource: Int? = null,
- val args: List,
- ) : UiText {
- constructor(
- language: Map,
- @StringRes fallbackResource: Int? = null,
- vararg args: Any,
- ) : this(language, fallbackResource, args.asList())
-
- fun Configuration.getStringForLocales(): String? {
- val locale = resolveLocale(language.keys)
- return language[locale?.language?.lowercase()] ?: language[locale?.language?.uppercase()]
- }
-
- private fun Configuration.resolveLocale(supportedLanguages: Set): Locale? {
- return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
- locales.getFirstMatch(supportedLanguages.toTypedArray())
- } else {
- @Suppress("DEPRECATION")
- locale
- }
- }
- }
-
- fun asString(resources: Resources) = when (this) {
- is DynamicString -> value
- is StringResource -> resources.getString(resId, *(args.toTypedArray()))
- is MultiLangString -> {
- val arguments = args.toTypedArray()
- resources.configuration.getStringForLocales()?.format(*arguments)
- ?: fallbackResource?.let { resources.getString(it, *arguments) }
- ?: throw Resources.NotFoundException("Could not find multilang string")
- }
- }
-}
diff --git a/dachlatten-android/src/main/kotlin/de/sipgate/dachlatten/android/text/UiTextExt.kt b/dachlatten-android/src/main/kotlin/de/sipgate/dachlatten/android/text/UiTextExt.kt
new file mode 100644
index 0000000..441c2f5
--- /dev/null
+++ b/dachlatten-android/src/main/kotlin/de/sipgate/dachlatten/android/text/UiTextExt.kt
@@ -0,0 +1,33 @@
+package de.sipgate.dachlatten.android.text
+
+import android.content.res.Configuration
+import android.content.res.Resources
+import android.os.Build
+import de.sipgate.dachlatten.text.TranslatedText
+import de.sipgate.dachlatten.text.UiText
+import java.util.Locale
+
+fun UiText.asString(resources: Resources) = when (this) {
+ is UiText.DynamicString -> value
+ is UiText.StringResource -> resources.getString(resId, *(args.toTypedArray()))
+ is UiText.MultiLangString -> {
+ val arguments = args.toTypedArray()
+ resources.configuration.getStringForLocales(language)?.format(*arguments)
+ ?: fallbackResource?.let { resources.getString(it, *arguments) }
+ ?: throw Resources.NotFoundException("Could not find multilang string")
+ }
+}
+
+private fun Configuration.getStringForLocales(translations: TranslatedText): String? {
+ val locale = resolveLocale(translations.keys)
+ return translations[locale?.language?.lowercase()] ?: translations[locale?.language?.uppercase()]
+}
+
+private fun Configuration.resolveLocale(supportedLanguages: Set): Locale? {
+ return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ locales.getFirstMatch(supportedLanguages.toTypedArray())
+ } else {
+ @Suppress("DEPRECATION")
+ locale
+ }
+}
diff --git a/dachlatten-android/src/test/kotlin/de/sipgate/dachlatten/android/text/UiTextTest.kt b/dachlatten-android/src/test/kotlin/de/sipgate/dachlatten/android/text/UiTextTest.kt
index 271712b..4cf9841 100644
--- a/dachlatten-android/src/test/kotlin/de/sipgate/dachlatten/android/text/UiTextTest.kt
+++ b/dachlatten-android/src/test/kotlin/de/sipgate/dachlatten/android/text/UiTextTest.kt
@@ -2,6 +2,7 @@ package de.sipgate.dachlatten.android.text
import android.content.res.Resources
import androidx.core.R
+import de.sipgate.dachlatten.text.UiText
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.assertThrows
diff --git a/dachlatten-compose/build.gradle.kts b/dachlatten-compose/build.gradle.kts
index 88285e9..a2f11f7 100644
--- a/dachlatten-compose/build.gradle.kts
+++ b/dachlatten-compose/build.gradle.kts
@@ -6,10 +6,10 @@ plugins {
}
dependencies {
- compileOnly(project(":dachlatten-android"))
+ compileOnly(project(":dachlatten-text"))
compileOnly(libs.androidx.compose.ui)
- testImplementation(project(":dachlatten-android"))
+ testImplementation(project(":dachlatten-text"))
testImplementation(libs.bundles.androidx.compose.ui.test)
}
diff --git a/dachlatten-compose/src/main/kotlin/de/sipgate/dachlatten/compose/text/UiTextExt.kt b/dachlatten-compose/src/main/kotlin/de/sipgate/dachlatten/compose/text/UiTextExt.kt
index 032a119..66fabc8 100644
--- a/dachlatten-compose/src/main/kotlin/de/sipgate/dachlatten/compose/text/UiTextExt.kt
+++ b/dachlatten-compose/src/main/kotlin/de/sipgate/dachlatten/compose/text/UiTextExt.kt
@@ -1,10 +1,14 @@
package de.sipgate.dachlatten.compose.text
+import android.content.res.Configuration
import android.content.res.Resources
+import android.os.Build
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.stringResource
-import de.sipgate.dachlatten.android.text.UiText
+import de.sipgate.dachlatten.text.TranslatedText
+import de.sipgate.dachlatten.text.UiText
+import java.util.Locale
@Composable
fun UiText.asString() = when (this) {
@@ -12,8 +16,22 @@ fun UiText.asString() = when (this) {
is UiText.StringResource -> stringResource(id = resId, formatArgs = args.toTypedArray())
is UiText.MultiLangString -> {
val arguments = args.toTypedArray()
- LocalConfiguration.current.getStringForLocales() ?.format(*arguments)
+ LocalConfiguration.current.getStringForLocales(language) ?.format(*arguments)
?: fallbackResource?.let { stringResource(id = it, formatArgs = arguments) }
?: throw Resources.NotFoundException("Could not find multilang string")
}
}
+
+private fun Configuration.getStringForLocales(translations: TranslatedText): String? {
+ val locale = resolveLocale(translations.keys)
+ return translations[locale?.language?.lowercase()] ?: translations[locale?.language?.uppercase()]
+}
+
+private fun Configuration.resolveLocale(supportedLanguages: Set): Locale? {
+ return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ locales.getFirstMatch(supportedLanguages.toTypedArray())
+ } else {
+ @Suppress("DEPRECATION")
+ locale
+ }
+}
diff --git a/dachlatten-compose/src/test/kotlin/de/sipgate/dachlatten/compose/text/UiTextTest.kt b/dachlatten-compose/src/test/kotlin/de/sipgate/dachlatten/compose/text/UiTextTest.kt
index 2fda3f2..27480ed 100644
--- a/dachlatten-compose/src/test/kotlin/de/sipgate/dachlatten/compose/text/UiTextTest.kt
+++ b/dachlatten-compose/src/test/kotlin/de/sipgate/dachlatten/compose/text/UiTextTest.kt
@@ -3,7 +3,7 @@ package de.sipgate.dachlatten.compose.text
import android.content.res.Resources
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.core.R
-import de.sipgate.dachlatten.android.text.UiText
+import de.sipgate.dachlatten.text.UiText
import org.junit.Rule
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
diff --git a/dachlatten-text/build.gradle.kts b/dachlatten-text/build.gradle.kts
new file mode 100644
index 0000000..5bbe0db
--- /dev/null
+++ b/dachlatten-text/build.gradle.kts
@@ -0,0 +1,9 @@
+plugins {
+ id("android-library-base")
+ id("android-library-unit-test")
+ id("android-library-release")
+}
+
+dependencies {
+ implementation(libs.androidx.annotation)
+}
diff --git a/dachlatten-text/src/main/kotlin/de/sipgate/dachlatten/text/UiText.kt b/dachlatten-text/src/main/kotlin/de/sipgate/dachlatten/text/UiText.kt
new file mode 100644
index 0000000..bd72967
--- /dev/null
+++ b/dachlatten-text/src/main/kotlin/de/sipgate/dachlatten/text/UiText.kt
@@ -0,0 +1,30 @@
+package de.sipgate.dachlatten.text
+
+import androidx.annotation.StringRes
+
+typealias TranslatedText = Map
+
+sealed interface UiText {
+ data class DynamicString(val value: String) : UiText
+
+ data class StringResource(
+ @StringRes val resId: Int,
+ val args: List,
+ ) : UiText {
+ constructor(
+ @StringRes resId: Int,
+ vararg args: Any) : this(resId, args.asList())
+ }
+
+ data class MultiLangString(
+ val language: TranslatedText,
+ @StringRes val fallbackResource: Int? = null,
+ val args: List,
+ ) : UiText {
+ constructor(
+ language: TranslatedText,
+ @StringRes fallbackResource: Int? = null,
+ vararg args: Any,
+ ) : this(language, fallbackResource, args.asList())
+ }
+}
diff --git a/settings.gradle.kts b/settings.gradle.kts
index 2743031..38def8f 100644
--- a/settings.gradle.kts
+++ b/settings.gradle.kts
@@ -29,3 +29,4 @@ include(":dachlatten-flow")
include(":dachlatten-google")
include(":dachlatten-primitives")
include(":dachlatten-retrofit")
+include(":dachlatten-text")