Open y9san9 opened 2 years ago
One thing I did in this situation was use the expect/actual for making your own localized()
.
// commonMain
@Composable
fun StringResource.localized(): String = StringDesc.Resource(this).localized()
@Composable
expect fun StringDesc.localized(): String
// androidMain
@Composable
actual fun StringDesc.localized(): String = this.toString(LocalContext.current)
// iosMain
@Composable
actual fun StringDesc.localized(): String = this.localized()
Then you can use it in common code like so.
@Composable
fun App() {
val formattedString: StringDesc = StringDesc.ResourceFormatted(
MR.strings.welcome_greeting,
args = listOf("Vader")
)
Column {
Text(formattedString.localized())
Text(MR.strings.my_string.localized())
}
}
@plusmobileapps you can just use resources-compose
module that already contains localized
for compose
Lol I didn't see that in the docs but that makes a lot more sense to use that directly. I guess I don't fully understand the use case then for the original issue posted 🤔
Sometimes it can be useful to get resources from common code. For such situations, a special type can be provided which will have no meaning in the common code, but which needs to be passed from the platform code to the localized() method.
Example:
So then you can use it for common ui:
Or something else