icerockdev / moko-resources

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

0.24.0-beta-4 incorrectly handles plurals with `"` character; displays as `\"` on iOS and doesn't display on Android. #710

Closed darronschall closed 4 months ago

darronschall commented 4 months ago

Given this plural:

<plural name="search__results_for">
    <item quantity="zero">%d results for "%s"</item>
    <item quantity="one">%d result for "%s"</item>
    <item quantity="two">%d results for "%s"</item>
    <item quantity="few">%d results for "%s"</item>
    <item quantity="many">%d results for "%s"</item>
    <item quantity="other">%d results for "%s"</item>
</plural>

iOS

The iOS plural generation in Localizable.stringsdict generates as:

<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>zero</key>
<string>%%d results for \"%%s\"</string>
<key>one</key>
<string>%%d result for \"%%s\"</string>
<key>two</key>
<string>%%d results for \"%%s\"</string>
<key>few</key>
<string>%%d results for \"%%s\"</string>
<key>many</key>
<string>%%d results for \"%%s\"</string>
<key>other</key>
<string>%%d results for \"%%s\"</string>

The quotes in those <string> elements should not be escaped. This is causing the string to show \" instead of just ":

Screenshot 2024-05-16 at 9 48 42 AM

Note that replacing the " with &quot; does not make a difference - the generated output is still \".

Android

For Android, the generated content in multiplatform_plurals.xml looks correct:

<plurals name="search__results_for">
    <item quantity="zero">%d results for &quot;%s&quot;</item>
    <item quantity="one">%d result for &quot;%s&quot;</item>
    <item quantity="two">%d results for &quot;%s&quot;</item>
    <item quantity="few">%d results for &quot;%s&quot;</item>
    <item quantity="many">%d results for &quot;%s&quot;</item>
    <item quantity="other">%d results for &quot;%s&quot;</item>
</plurals>

However, when using MR.plurals.search__results_for.format(0, 0, "Example"), the resulting string drops the quotes entirely:

Screenshot 2024-05-16 at 10 22 20 AM

Is this an Android issue somewhere inside PluralFormattedStringDesc?

ExNDY commented 4 months ago

Hm, i'm not sure in android plurals, because i'm try two variants in native:

<resources>
    <plurals name="android_res_test">
        <item quantity="zero">zero chars &quot;%s&quot;</item>
        <item quantity="one">%d char &quot;%s&quot;</item>
        <item quantity="two">%d chars &quot;%s&quot;</item>
        <item quantity="other">%d chars &quot;%s&quot;</item>
    </plurals>
</resources>

And:

<resources>
    <plurals name="android_res_test">
        <item quantity="zero">zero chars "%s"</item>
        <item quantity="one">%d char "%s"</item>
        <item quantity="two">%d chars "%s"</item>
        <item quantity="other">%d chars "%s"</item>
    </plurals>
</resources>

Call inside activity:

this.resources.getQuantityString(R.plurals.android_res_test, 2, 2,"Same")

For all variants i'm get string without quotes. If i'm add mirror for quotes: like: \"%s\" or \&quot;%s\&quot; then i'm get quoting around Same.

start of my research on android: https://developer.android.com/guide/topics/resources/string-resource

And if i'm add mirror in plurals of moko-resource it will be fine for android, but other platform has incorrect mirror for quot. Thanks for info

ExNDY commented 4 months ago

will be fixed in 0.24.0-beta-5