Browse Source

Rename QuantityStringResource to PluralStringResource for consistency

pull/4519/head
Chanjung Kim 8 months ago
parent
commit
700393edf2
  1. 32
      components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResources.kt
  2. 26
      components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt
  3. 2
      components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/TestUtils.kt
  4. 8
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt
  5. 6
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt

32
components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResources.kt

@ -38,7 +38,7 @@ class StringResource
@OptIn(InternalResourceApi::class)
@ExperimentalResourceApi
@Immutable
class QuantityStringResource
class PluralStringResource
@InternalResourceApi constructor(id: String, val key: String, items: Set<ResourceItem>) : Resource(id, items)
private sealed interface StringItem {
@ -197,10 +197,10 @@ private suspend fun loadString(
*/
@ExperimentalResourceApi
@Composable
fun pluralStringResource(resource: QuantityStringResource, quantity: Int): String {
fun pluralStringResource(resource: PluralStringResource, quantity: Int): String {
val resourceReader = LocalResourceReader.current
val pluralStr by rememberResourceState(resource, quantity, { "" }) { env ->
loadQuantityString(resource, quantity, resourceReader, env)
loadPluralString(resource, quantity, resourceReader, env)
}
return pluralStr
}
@ -215,12 +215,12 @@ fun pluralStringResource(resource: QuantityStringResource, quantity: Int): Strin
* @throws IllegalArgumentException If the provided ID or the pluralization is not found in the resource file.
*/
@ExperimentalResourceApi
suspend fun getQuantityString(resource: QuantityStringResource, quantity: Int): String =
loadQuantityString(resource, quantity, DefaultResourceReader, getResourceEnvironment())
suspend fun getPluralString(resource: PluralStringResource, quantity: Int): String =
loadPluralString(resource, quantity, DefaultResourceReader, getResourceEnvironment())
@OptIn(InternalResourceApi::class, ExperimentalResourceApi::class)
private suspend fun loadQuantityString(
resource: QuantityStringResource,
private suspend fun loadPluralString(
resource: PluralStringResource,
quantity: Int,
resourceReader: ResourceReader,
environment: ResourceEnvironment
@ -251,13 +251,13 @@ private suspend fun loadQuantityString(
*/
@ExperimentalResourceApi
@Composable
fun pluralStringResource(resource: QuantityStringResource, quantity: Int, vararg formatArgs: Any): String {
fun pluralStringResource(resource: PluralStringResource, quantity: Int, vararg formatArgs: Any): String {
val resourceReader = LocalResourceReader.current
val args = formatArgs.map { it.toString() }
val quantityStr by rememberResourceState(resource, quantity, args, { "" }) { env ->
loadQuantityString(resource, quantity, args, resourceReader, env)
val pluralStr by rememberResourceState(resource, quantity, args, { "" }) { env ->
loadPluralString(resource, quantity, args, resourceReader, env)
}
return quantityStr
return pluralStr
}
/**
@ -271,8 +271,8 @@ fun pluralStringResource(resource: QuantityStringResource, quantity: Int, vararg
* @throws IllegalArgumentException If the provided ID or the pluralization is not found in the resource file.
*/
@ExperimentalResourceApi
suspend fun getQuantityString(resource: QuantityStringResource, quantity: Int, vararg formatArgs: Any): String =
loadQuantityString(
suspend fun getPluralString(resource: PluralStringResource, quantity: Int, vararg formatArgs: Any): String =
loadPluralString(
resource, quantity,
formatArgs.map { it.toString() },
DefaultResourceReader,
@ -280,14 +280,14 @@ suspend fun getQuantityString(resource: QuantityStringResource, quantity: Int, v
)
@OptIn(ExperimentalResourceApi::class)
private suspend fun loadQuantityString(
resource: QuantityStringResource,
private suspend fun loadPluralString(
resource: PluralStringResource,
quantity: Int,
args: List<String>,
resourceReader: ResourceReader,
environment: ResourceEnvironment
): String {
val str = loadQuantityString(resource, quantity, resourceReader, environment)
val str = loadPluralString(resource, quantity, resourceReader, environment)
return str.replaceWithArgs(args)
}

26
components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt

@ -145,9 +145,9 @@ class ComposeResourceTest {
}
@Test
fun testQuantityStringResourceCache() = runComposeUiTest {
fun testPluralStringResourceCache() = runComposeUiTest {
val testResourceReader = TestResourceReader()
var res by mutableStateOf(TestQuantityStringResource("plurals"))
var res by mutableStateOf(TestPluralStringResource("plurals"))
var quantity by mutableStateOf(0)
var str = ""
setContent {
@ -177,7 +177,7 @@ class ComposeResourceTest {
assertEquals("other", str)
assertEquals(3, quantity)
res = TestQuantityStringResource("another_plurals")
res = TestPluralStringResource("another_plurals")
quantity = 0
waitForIdle()
assertEquals("another other", str)
@ -188,13 +188,13 @@ class ComposeResourceTest {
}
@Test
fun testReadQuantityStringResource() = runComposeUiTest {
fun testReadPluralStringResource() = runComposeUiTest {
var plurals = ""
var another_plurals = ""
setContent {
CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) {
plurals = pluralStringResource(TestQuantityStringResource("plurals"), 1)
another_plurals = pluralStringResource(TestQuantityStringResource("another_plurals"), 1)
plurals = pluralStringResource(TestPluralStringResource("plurals"), 1)
another_plurals = pluralStringResource(TestPluralStringResource("another_plurals"), 1)
}
}
waitForIdle()
@ -213,8 +213,8 @@ class ComposeResourceTest {
var str2 = ""
setContent {
CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) {
str1 = pluralStringResource(TestQuantityStringResource("messages"), quantity, 3, arg)
str2 = pluralStringResource(TestQuantityStringResource("messages"), quantity, 5, arg)
str1 = pluralStringResource(TestPluralStringResource("messages"), quantity, 3, arg)
str2 = pluralStringResource(TestPluralStringResource("messages"), quantity, 5, arg)
}
}
waitForIdle()
@ -233,11 +233,11 @@ class ComposeResourceTest {
}
@Test
fun testLoadQuantityStringResource() = runTest {
assertEquals("one", getQuantityString(TestQuantityStringResource("plurals"), 1))
assertEquals("other", getQuantityString(TestQuantityStringResource("plurals"), 5))
assertEquals("another one", getQuantityString(TestQuantityStringResource("another_plurals"), 1))
assertEquals("another other", getQuantityString(TestQuantityStringResource("another_plurals"), 5))
fun testLoadPluralStringResource() = runTest {
assertEquals("one", getPluralString(TestPluralStringResource("plurals"), 1))
assertEquals("other", getPluralString(TestPluralStringResource("plurals"), 5))
assertEquals("another one", getPluralString(TestPluralStringResource("another_plurals"), 1))
assertEquals("another other", getPluralString(TestPluralStringResource("another_plurals"), 5))
}
@Test

2
components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/TestUtils.kt

@ -12,7 +12,7 @@ internal fun TestStringResource(key: String) = StringResource(
)
@OptIn(InternalResourceApi::class, ExperimentalResourceApi::class)
internal fun TestQuantityStringResource(key: String) = QuantityStringResource(
internal fun TestPluralStringResource(key: String) = PluralStringResource(
"PLURALS:$key",
key,
setOf(ResourceItem(emptySet(), "strings.xml"))

8
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt

@ -102,11 +102,11 @@ internal abstract class GenerateResClassTask : DefaultTask() {
if (typeString == "values" && file.name.equals("strings.xml", true)) {
val stringIds = getStringIds(file)
val quantityStringIds = getQuantityStringIds(file)
val pluralStringIds = getPluralStringIds(file)
return stringIds.map { strId ->
ResourceItem(ResourceType.STRING, qualifiers, strId.asUnderscoredIdentifier(), path)
} + quantityStringIds.map { quantityStrId ->
ResourceItem(ResourceType.QUANTITY_STRING, qualifiers, quantityStrId.asUnderscoredIdentifier(), path)
} + pluralStringIds.map { pluralStrId ->
ResourceItem(ResourceType.PLURAL_STRING, qualifiers, pluralStrId.asUnderscoredIdentifier(), path)
}
}
@ -124,7 +124,7 @@ internal abstract class GenerateResClassTask : DefaultTask() {
return ids.toSet()
}
private fun getQuantityStringIds(stringsXml: File): Set<String> {
private fun getPluralStringIds(stringsXml: File): Set<String> {
val doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stringsXml)
val items = doc.getElementsByTagName("resources").item(0).childNodes
val ids = List(items.length) { items.item(it) }

6
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt

@ -9,7 +9,7 @@ import kotlin.io.path.invariantSeparatorsPathString
internal enum class ResourceType(val typeName: String) {
DRAWABLE("drawable"),
STRING("string"),
QUANTITY_STRING("plurals"),
PLURAL_STRING("plurals"),
FONT("font");
override fun toString(): String = typeName
@ -32,7 +32,7 @@ internal data class ResourceItem(
private fun ResourceType.getClassName(): ClassName = when (this) {
ResourceType.DRAWABLE -> ClassName("org.jetbrains.compose.resources", "DrawableResource")
ResourceType.STRING -> ClassName("org.jetbrains.compose.resources", "StringResource")
ResourceType.QUANTITY_STRING -> ClassName("org.jetbrains.compose.resources", "QuantityStringResource")
ResourceType.PLURAL_STRING -> ClassName("org.jetbrains.compose.resources", "PluralStringResource")
ResourceType.FONT -> ClassName("org.jetbrains.compose.resources", "FontResource")
}
@ -227,7 +227,7 @@ private fun getChunkFileSpec(
CodeBlock.builder()
.add("return %T(\n", type.getClassName()).withIndent {
add("\"${type}:${resName}\",")
if (type == ResourceType.STRING || type == ResourceType.QUANTITY_STRING) add(" \"$resName\",")
if (type == ResourceType.STRING || type == ResourceType.PLURAL_STRING) add(" \"$resName\",")
withIndent {
add("\nsetOf(\n").withIndent {
items.forEach { item ->

Loading…
Cancel
Save