I have the below
<string name="loading_listing">Loading listings %d</string>
<plurals name="loading_listing">
<item quantity="one">Found %d listing</item>
<item quantity="other">Found %d listings</item>
</plurals>
If I use the string
, as below all is okay
Text(text = stringResource(R.string.loading_listing, 1))
But if I use the plurals
,
Text(text = stringResource(R.plurals.loading_listing, 1))
it will crash
android.content.res.Resources$NotFoundException: String resource ID #0x7f0d0003
at android.content.res.Resources.getText(Resources.java:444)
at android.content.res.Resources.getString(Resources.java:537)
at android.content.res.Resources.getString(Resources.java:561)
at androidx.compose.ui.res.StringResources_androidKt.stringResource(StringResources.android.kt:48)
I think it is not stringResource(...)
that I can use. What is it then?
Compose 1.2
From Jetpack Compose 1.2 there's a pluralStringResource
function.
pluralStringResource(id = R.plurals.loading_listing, count = 1)
In this version it is an experimental API, so you need to add the annotation @ExperimentalComposeUiApi
.
Pre 1.2 Answer
Meanwhile it's not available in Compose lib, you can create a helper function for that...
@Composable
fun pluralResource(
@PluralsRes resId: Int,
quantity: Int,
vararg formatArgs: Any? = emptyArray()
): String {
return LocalContext.current.resources
.getQuantityString(resId, quantity, *formatArgs)
}
Starting with Compose 1.2 you can use the pluralStringResource
function.
pluralStringResource(R.plurals.plurals_array, 1)
With 1.1.x
there isn't a function to get a plural resource.
You can use something like:
val resources = LocalContext.current.resources
Text(
text = resources.getQuantityString(
R.plurals.loading_listing, 0, 10
)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With