Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Plural resource in JetpackCompose String

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?

like image 881
Elye Avatar asked Sep 13 '25 16:09

Elye


2 Answers

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)
}
like image 109
nglauber Avatar answered Sep 15 '25 18:09

nglauber


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
    )
)
like image 21
Gabriele Mariotti Avatar answered Sep 15 '25 16:09

Gabriele Mariotti