icerockdev / moko-resources

Resources access for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev/
Apache License 2.0
1.07k stars 120 forks source link

Formatted strings in iOS with $s placeholders #480

Open cw-communiski opened 1 year ago

cw-communiski commented 1 year ago

I cannot figure out how to get strings with placeholders in the iOS app. I believe the issue I'm having is due to the strings using placeholders in the format "%1$s" when iOS requires the format "%1$@"

This is how I'm getting the strings in swiftUI at present: let string = SharedRes.strings().text_dont_have_account.localized()

When I try to replace this with: let string = String(format:SharedRes.strings().text_dont_have_account.localized(), arguments:["TEST"]) I get an error.

Here's the string resource: <string name="text_dont_have_account">Don\'t have an account? %1$s</string> and if I change this to <string name="text_dont_have_account">Don\'t have an account? %1$@</string> It works on iOS, but causes the Android app to crash.

What am I doing wrong here? How do I use strings with placeholders across both platforms?

Alex009 commented 1 year ago

hi! in past we implement support of positional arguments and it works. https://github.com/icerockdev/moko-resources/blob/15fadd5ff058a967b0980daaebc48bb76e6cf465/samples/resources-gallery/mpp-library/src/commonMain/resources/MR/base/strings.xml#L6 here resource declaration https://github.com/icerockdev/moko-resources/blob/15fadd5ff058a967b0980daaebc48bb76e6cf465/samples/resources-gallery/mpp-library/src/commonTest/kotlin/com/icerock/library/StringResourceTests.kt#L46 here test that run on every pull request on android & ios. last run shows successful test result

image

please compare your implementation with samples. if you not find solution - please create minimal project reproducer

geckogecko commented 6 months ago

I have the exact same problem as @cw-communiski. I guess that we are not using localized strings on the ios side as they are planned to be used.

@Alex009 can you give an example on how to use formatted strings on ios? (The readme only shows how to use them from common part.)