Introduce simplePluralStringResource methods, as Composable and in StringProvider.
This commit is contained in:
parent
1195499eb7
commit
8c02d873d8
8 changed files with 104 additions and 10 deletions
|
|
@ -33,6 +33,7 @@ dependencies {
|
||||||
implementation(projects.libraries.matrixui)
|
implementation(projects.libraries.matrixui)
|
||||||
implementation(projects.libraries.designsystem)
|
implementation(projects.libraries.designsystem)
|
||||||
implementation(projects.libraries.uiStrings)
|
implementation(projects.libraries.uiStrings)
|
||||||
|
implementation(projects.libraries.uiUtils)
|
||||||
implementation(projects.libraries.androidutils)
|
implementation(projects.libraries.androidutils)
|
||||||
implementation(projects.libraries.usersearch.api)
|
implementation(projects.libraries.usersearch.api)
|
||||||
implementation(libs.coil.compose)
|
implementation(libs.coil.compose)
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,7 @@ import io.element.android.libraries.matrix.ui.components.SelectedUsersRowList
|
||||||
import io.element.android.libraries.matrix.ui.model.getAvatarData
|
import io.element.android.libraries.matrix.ui.model.getAvatarData
|
||||||
import io.element.android.libraries.matrix.ui.model.getBestName
|
import io.element.android.libraries.matrix.ui.model.getBestName
|
||||||
import io.element.android.libraries.ui.strings.CommonStrings
|
import io.element.android.libraries.ui.strings.CommonStrings
|
||||||
|
import io.element.android.libraries.ui.utils.strings.simplePluralStringResource
|
||||||
import kotlinx.collections.immutable.ImmutableList
|
import kotlinx.collections.immutable.ImmutableList
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
|
@ -263,16 +264,16 @@ private fun InvitePeopleConfirmModal(
|
||||||
dragHandle = null,
|
dragHandle = null,
|
||||||
) {
|
) {
|
||||||
IconTitleSubtitleMolecule(
|
IconTitleSubtitleMolecule(
|
||||||
title = if (users.size > 1) {
|
title = simplePluralStringResource(
|
||||||
stringResource(R.string.screen_invite_users_confirm_dialog_title_mutiple_users)
|
resIdForOne = R.string.screen_invite_users_confirm_dialog_title_one_user,
|
||||||
} else {
|
resIdForOthers = R.string.screen_invite_users_confirm_dialog_title_mutiple_users,
|
||||||
stringResource(R.string.screen_invite_users_confirm_dialog_title_one_user)
|
count = users.size,
|
||||||
},
|
),
|
||||||
subTitle = if (users.size > 1) {
|
subTitle = simplePluralStringResource(
|
||||||
stringResource(R.string.screen_invite_users_confirm_dialog_subtitle_multiple_users)
|
resIdForOne = R.string.screen_invite_users_confirm_dialog_subtitle_one_user,
|
||||||
} else {
|
resIdForOthers = R.string.screen_invite_users_confirm_dialog_subtitle_multiple_users,
|
||||||
stringResource(R.string.screen_invite_users_confirm_dialog_subtitle_one_user)
|
count = users.size,
|
||||||
},
|
),
|
||||||
iconStyle = BigIcon.Style.Default(CompoundIcons.UserAddSolid()),
|
iconStyle = BigIcon.Style.Default(CompoundIcons.UserAddSolid()),
|
||||||
modifier = Modifier.padding(
|
modifier = Modifier.padding(
|
||||||
top = 32.dp,
|
top = 32.dp,
|
||||||
|
|
|
||||||
|
|
@ -27,4 +27,9 @@ class PreviewStringProvider(
|
||||||
override fun getQuantityString(@PluralsRes resId: Int, quantity: Int, vararg formatArgs: Any?): String {
|
override fun getQuantityString(@PluralsRes resId: Int, quantity: Int, vararg formatArgs: Any?): String {
|
||||||
return resources.getQuantityString(resId, quantity, *formatArgs)
|
return resources.getQuantityString(resId, quantity, *formatArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getSimpleQuantityString(resIdForOne: Int, resIdForOthers: Int, quantity: Int, vararg formatArgs: Any?): String {
|
||||||
|
val resId = if (quantity == 1) resIdForOne else resIdForOthers
|
||||||
|
return resources.getString(resId, *formatArgs)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2026 Element Creations Ltd.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||||
|
* Please see LICENSE files in the repository root for full details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.libraries.ui.utils.strings
|
||||||
|
|
||||||
|
import androidx.annotation.StringRes
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.ReadOnlyComposable
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Similar to [androidx.compose.ui.res.pluralStringResource] but with separate resource ids for singular and plural values.
|
||||||
|
* Useful when we want to use different strings for singular and plural forms but not mentioning the actual quantity in the string.
|
||||||
|
* In this case, we cannot use getQuantityString, because some locales have more than two plural forms, and require the quantity to
|
||||||
|
* be part of the resulting strings.
|
||||||
|
* @param resIdForOne Resource id for the case when [count] is 1.
|
||||||
|
* @param resIdForOthers Resource id for the other cases ([count] is not 1).
|
||||||
|
* @param count The quantity to determine whether to use singular or plural form. Must be greater than or equal to 1.
|
||||||
|
* @param formatArgs The format arguments that will be used for substitution in the resulting string. Will be applied to either
|
||||||
|
* the singular or plural string depending on the quantity.
|
||||||
|
* @return The localized string corresponding to the given quantity.
|
||||||
|
*/
|
||||||
|
@Composable
|
||||||
|
@ReadOnlyComposable
|
||||||
|
fun simplePluralStringResource(
|
||||||
|
@StringRes resIdForOne: Int,
|
||||||
|
@StringRes resIdForOthers: Int,
|
||||||
|
count: Int,
|
||||||
|
vararg formatArgs: Any,
|
||||||
|
): String {
|
||||||
|
val resId = if (count == 1) resIdForOne else resIdForOthers
|
||||||
|
return stringResource(resId, *formatArgs)
|
||||||
|
}
|
||||||
|
|
@ -34,5 +34,30 @@ interface StringProvider {
|
||||||
* stripped of styled text information.
|
* stripped of styled text information.
|
||||||
*/
|
*/
|
||||||
fun getString(@StringRes resId: Int, vararg formatArgs: Any?): String
|
fun getString(@StringRes resId: Int, vararg formatArgs: Any?): String
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a localized formatted string from the application's package's
|
||||||
|
* default string table, substituting the format arguments as defined in
|
||||||
|
* [java.util.Formatter] and [java.lang.String.format], based on the given quantity.
|
||||||
|
*/
|
||||||
fun getQuantityString(@PluralsRes resId: Int, quantity: Int, vararg formatArgs: Any?): String
|
fun getQuantityString(@PluralsRes resId: Int, quantity: Int, vararg formatArgs: Any?): String
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Similar to [getQuantityString] but with separate resource ids for singular and plural values.
|
||||||
|
* Useful when we want to use different strings for singular and plural forms but not mentioning the actual quantity in the string.
|
||||||
|
* In this case, we cannot use getQuantityString, because some locales have more than two plural forms, and require the quantity to
|
||||||
|
* be part of the resulting strings.
|
||||||
|
* @param resIdForOne Resource id for the case when [quantity] is 1.
|
||||||
|
* @param resIdForOthers Resource id for the other cases ([quantity] is not 1).
|
||||||
|
* @param quantity The quantity to determine whether to use singular or plural form. Must be greater than or equal to 1.
|
||||||
|
* @param formatArgs The format arguments that will be used for substitution in the resulting string. Will be applied to either
|
||||||
|
* the singular or plural string depending on the quantity.
|
||||||
|
* @return The localized string corresponding to the given quantity.
|
||||||
|
*/
|
||||||
|
fun getSimpleQuantityString(
|
||||||
|
@StringRes resIdForOne: Int,
|
||||||
|
@StringRes resIdForOthers: Int,
|
||||||
|
quantity: Int,
|
||||||
|
vararg formatArgs: Any?,
|
||||||
|
): String
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,4 +28,14 @@ class AndroidStringProvider(private val resources: Resources) : StringProvider {
|
||||||
override fun getQuantityString(@PluralsRes resId: Int, quantity: Int, vararg formatArgs: Any?): String {
|
override fun getQuantityString(@PluralsRes resId: Int, quantity: Int, vararg formatArgs: Any?): String {
|
||||||
return resources.getQuantityString(resId, quantity, *formatArgs)
|
return resources.getQuantityString(resId, quantity, *formatArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getSimpleQuantityString(
|
||||||
|
resIdForOne: Int,
|
||||||
|
resIdForOthers: Int,
|
||||||
|
quantity: Int,
|
||||||
|
vararg formatArgs: Any?,
|
||||||
|
): String {
|
||||||
|
val resId = if (quantity == 1) resIdForOne else resIdForOthers
|
||||||
|
return resources.getString(resId, *formatArgs)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,4 +28,14 @@ class FakeStringProvider(
|
||||||
lastResIdParam = resId
|
lastResIdParam = resId
|
||||||
return defaultResult + " ($quantity) " + formatArgs.joinToString()
|
return defaultResult + " ($quantity) " + formatArgs.joinToString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getSimpleQuantityString(
|
||||||
|
resIdForOne: Int,
|
||||||
|
resIdForOthers: Int,
|
||||||
|
quantity: Int,
|
||||||
|
vararg formatArgs: Any?,
|
||||||
|
): String {
|
||||||
|
lastResIdParam = if (quantity == 1) resIdForOne else resIdForOthers
|
||||||
|
return defaultResult + " ($quantity) " + formatArgs.joinToString()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,4 +24,9 @@ class InstrumentationStringProvider : StringProvider {
|
||||||
override fun getQuantityString(resId: Int, quantity: Int, vararg formatArgs: Any?): String {
|
override fun getQuantityString(resId: Int, quantity: Int, vararg formatArgs: Any?): String {
|
||||||
return resource.getQuantityString(resId, quantity, *formatArgs)
|
return resource.getQuantityString(resId, quantity, *formatArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getSimpleQuantityString(resIdForOne: Int, resIdForOthers: Int, quantity: Int, vararg formatArgs: Any?): String {
|
||||||
|
val resId = if (quantity == 1) resIdForOne else resIdForOthers
|
||||||
|
return resource.getString(resId, *formatArgs)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue