Joel Denke
01/09/2024, 2:11 PMinterface MyStrings {
val actionSave: MyTextResource
val actionEdit: MyTextResource
val actionLogout: MyTextResource
val actionRegister: MyTextResource
val actionOk: MyTextResource
val actionCancel: MyTextResource
val actionLogin: MyTextResource
val actionClose: MyTextResource
}
MyTextResource is a sealed interface to support strings, annotations, urls, plurals and whatever type each platform will end up having in future.
I am using it in combination with MyText composable enforce everywhere.
And then depending on current Locale I am switch implementation for each language we support.
I think it semi aligns with approach of this library https://github.com/adrielcafe/lyricist
I am not yet confident which direction to use as Jetbrains not very clear on their roadmap of future things of resources, including images and such.
Hence doing my own wrappers for images, locales and such to be sure I can easy change thing without refactoring the entire app π
Sample usage:
MyText(
text = MyApp.strings.actionLogin,
style = MyApp.typography.button,
)
MyApp is an object with @ReadOnlyComposables mapped to LocalCompositions.Pablichjenkov
01/09/2024, 2:34 PMcurioustechizen
01/09/2024, 3:17 PMPablichjenkov
01/09/2024, 3:23 PMPablichjenkov
01/09/2024, 3:24 PMJoel Denke
01/09/2024, 3:26 PMJoel Denke
01/09/2024, 3:27 PMPablichjenkov
01/09/2024, 3:35 PMPablichjenkov
01/09/2024, 3:37 PMJoel Denke
01/09/2024, 3:38 PMPablichjenkov
01/09/2024, 3:38 PMJoel Denke
01/09/2024, 3:39 PMPablichjenkov
01/09/2024, 3:39 PMJoel Denke
01/09/2024, 3:40 PMPablichjenkov
01/09/2024, 3:42 PMJoel Denke
01/09/2024, 3:51 PMclass AndroidString(
@StringRes val stringResId: Int,
private vararg val formatArgs: Any
) : TextResource {
@Composable
override fun getString(): String {
val resources = LocalContext.current.resources
return resources.getString(stringResId, *formatArgs)
}
}
class AndroidPlural(
@PluralsRes val pluralResId: Int,
private val count: Int,
private vararg val formatArgs: Any
) : TextResource {
@Composable
override fun getString(): String {
val resources = LocalContext.current.resources
return resources.getQuantityString(pluralResId, count, formatArgs)
}
}
Will maybe give it a go using resource("path.xml") or such. Any ideas is welcome πPablichjenkov
01/09/2024, 4:52 PMPablichjenkov
01/09/2024, 4:54 PMgetString()
and getQuantityString()
? If so you are done.Joel Denke
01/09/2024, 5:09 PMPablichjenkov
01/09/2024, 5:20 PMJoel Denke
01/09/2024, 5:50 PMJoel Denke
01/09/2024, 6:00 PMPablichjenkov
01/09/2024, 6:11 PMJoel Denke
01/10/2024, 12:10 PMPablichjenkov
01/10/2024, 12:57 PMJoel Denke
01/10/2024, 1:20 PMJoel Denke
01/10/2024, 1:22 PMJoel Denke
01/10/2024, 1:22 PM