Skip to content

Commit

Permalink
Merge pull request #801 from Tetraquark/#800
Browse files Browse the repository at this point in the history
#800 Fix ResourceFormattedStringDesc.localized with StringDesc args
  • Loading branch information
Alex009 authored Feb 2, 2025
2 parents 0b16b7a + 79bfd4a commit d26537d
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 11 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ buildscript {
}
dependencies {
classpath "dev.icerock.moko:resources-generator:0.24.4"
classpath "dev.icerock.moko:resources-generator:0.24.5"
}
}
Expand All @@ -82,10 +82,10 @@ project build.gradle
apply plugin: "dev.icerock.mobile.multiplatform-resources"
dependencies {
commonMainApi("dev.icerock.moko:resources:0.24.4")
commonMainApi("dev.icerock.moko:resources-compose:0.24.4") // for compose multiplatform
commonMainApi("dev.icerock.moko:resources:0.24.5")
commonMainApi("dev.icerock.moko:resources-compose:0.24.5") // for compose multiplatform
commonTestImplementation("dev.icerock.moko:resources-test:0.24.4")
commonTestImplementation("dev.icerock.moko:resources-test:0.24.5")
}
multiplatformResources {
Expand Down Expand Up @@ -132,7 +132,7 @@ should [add `export` declarations](https://kotlinlang.org/docs/multiplatform-bui

```
framework {
export("dev.icerock.moko:resources:0.24.4")
export("dev.icerock.moko:resources:0.24.5")
export("dev.icerock.moko:graphics:0.9.0") // toUIColor here
}
```
Expand Down
2 changes: 1 addition & 1 deletion gradle/moko.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
resourcesVersion = "0.24.4"
resourcesVersion = "0.24.5"

[libraries]
resources = { module = "dev.icerock.moko:resources", version.ref = "resourcesVersion" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ actual data class PluralFormattedStringDesc actual constructor(
override suspend fun toLocalizedString(): String =
toLocalizedString(pluralsRes.loader.getOrLoad())

@Suppress("SpreadOperator")
override fun toLocalizedString(provider: JsStringProvider): String {
return pluralsRes.localized(
provider = provider,
locale = StringDesc.localeType.locale,
quantity = number,
args = args.toTypedArray()
*Utils.processArgs(args, provider)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ actual data class ResourceFormattedStringDesc actual constructor(
override suspend fun toLocalizedString(): String =
toLocalizedString(stringRes.loader.getOrLoad())

@Suppress("SpreadOperator")
override fun toLocalizedString(provider: JsStringProvider): String {
return stringRes.localized(
provider = provider,
locale = StringDesc.localeType.locale,
args = args.toTypedArray()
*Utils.processArgs(args, provider)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright 2025 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.resources.desc

import dev.icerock.moko.resources.provider.JsStringProvider

object Utils {
fun processArgs(
args: List<Any>,
provider: JsStringProvider
): Array<out Any> {
return args.map { (it as? StringDesc)?.toLocalizedString(provider) ?: it }.toTypedArray()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ actual data class PluralFormattedStringDesc actual constructor(
val number: Int,
val args: List<Any>
) : StringDesc {

@Suppress("SpreadOperator")
override fun localized() = pluralsRes.localized(
locale = StringDesc.localeType.currentLocale,
quantity = number,
*(args.toTypedArray())
*Utils.processArgs(args)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ actual data class ResourceFormattedStringDesc actual constructor(
private val stringRes: StringResource,
private val args: List<Any>
) : StringDesc {
@Suppress("SpreadOperator")
override fun localized() = stringRes.localized(
locale = StringDesc.localeType.currentLocale,
*(args.toTypedArray())
*Utils.processArgs(args)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright 2025 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.resources.desc

object Utils {
fun processArgs(args: List<Any>): Array<out Any> {
return args.map { (it as? StringDesc)?.localized() ?: it }.toTypedArray()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<string name="quotes">Alex009 said "hello world" &amp; "write tests".</string>
<string name="single_quotes">Alex009 said 'hello'</string>
<string name="text_only_in_base">Text from base locale</string>
<string name="hello_formatted_string">Hello %s</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
<string name="quotes">Alex009 сказал "привет мир" &amp; "пишите тесты".</string>
<string name="single_quotes">Alex009 сказал 'привет'</string>
<string name="test.dialect">Russian</string>
<string name="hello_formatted_string">Привет %s</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ class StringResourceEnTests : BaseStringResourceTests("en") {
expected = "Test data 2",
actual = MR.strings.format.format(2)
)

@Test
fun checkFormattedStringWithStringDesc() = stringTest(
expected = "Hello Test Project",
actual = MR.strings.hello_formatted_string.format(
MR.strings.common_name.desc()
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ class StringResourceRuTests : BaseStringResourceTests("ru") {
expected = "Тестовые данные 2",
actual = MR.strings.format.format(2)
)

@Test
fun checkFormattedStringWithStringDesc() = stringTest(
expected = "Привет Тестовый проект",
actual = MR.strings.hello_formatted_string.format(
MR.strings.common_name.desc()
)
)
}

0 comments on commit d26537d

Please sign in to comment.