JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
15.24k stars 1.11k forks source link

Support arbitrary string resource ID #4861

Open paxbun opened 1 month ago

paxbun commented 1 month ago

On SwiftUI, when a user writes a code like the following:

Text("This is a sample text.")

SwiftUI automatically maps "This is a sample text" to another string defined in Localizable.strings. This can be done because Localizable.strings allows any string to be a key.

# Localizable.strings (Korean)
"This is a sample text." = "샘플 텍스트입니다."

This can also be achieved in Kotlin theoretically, as Kotlin supports non-standard identifiers with backticks (except for the unsupported ones due to JVM).

Text(stringResource(Res.strings.`This is a sample text`))
<string id="This is a sample text">This is a sample text.</string>
<string id="This is a sample text">샘플 텍스트입니다.</string>

Although on Android string IDs have used snake_case, and the IDE replaced the occurrences of such IDs with the actual content when rendering the code, allowing this helps programmers to worry about resource ID namings less and eventually improves their productivity. One can do something like:

@Composable
fun MyView(value: String) {
  Text(stringResource(Res.strings.`MyView!The value is`, value))
}

so the human translators can easily find where and how the text is actually used.

However, the current implementation does not consider the case where non-standard identifiers are used as string resource IDs. To achieve this, $resName in the following should be escaped using the %N format specifier of Kotlinpoet's CodeBlock.

https://github.com/JetBrains/compose-multiplatform/blob/b32350459acceb9cca6b9e4422b7aaa051d9ae7d/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GeneratedResClassSpec.kt#L243

https://github.com/JetBrains/compose-multiplatform/blob/b32350459acceb9cca6b9e4422b7aaa051d9ae7d/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GeneratedResClassSpec.kt#L264

I'm not sure whether this is intended or not, so I file a new issue here.