How to add regular string placeholders to a translated plurals .stringdict in swift ios

后端 未结 1 959
时光说笑
时光说笑 2020-12-21 06:22

I want to translate this string using a plurar stringdict in swift for iOS

  • stays at %1$@
  • stay at %1$@

Using a simple plural without pla

相关标签:
1条回答
  • 2020-12-21 07:01

    Positional parameters n$ are one-based, so in

    let label = String.localizedStringWithFormat(format, kidsIds.count, "Name")
    

    "Name" is the second parameter, and you reference it with %2$@:

    <key>NSStringLocalizedFormatKey</key>
    <string>%#@format@</string>
    <key>format</key>
    <dict>
        <key>NSStringFormatSpecTypeKey</key>
        <string>NSStringPluralRuleType</string>
        <key>NSStringFormatValueTypeKey</key>
        <string>li</string>
        <key>one</key>
        <string>Sleeps at %2$@</string>
        <key>other</key>
        <string>Sleep at %2$@</string>
    </dict>
    

    In your code, %1$@ refers to the first argument kidsIds.count. That is not a string which leads to the crash.

    Alternatively, put it into the NSStringLocalizedFormatKey:

    <key>NSStringLocalizedFormatKey</key>
    <string>%#@format@ at %@</string>
    <key>format</key>
    <dict>
        <key>NSStringFormatSpecTypeKey</key>
        <string>NSStringPluralRuleType</string>
        <key>NSStringFormatValueTypeKey</key>
        <string>li</string>
        <key>one</key>
        <string>Sleeps</string>
        <key>other</key>
        <string>Sleep</string>
    </dict>
    
    0 讨论(0)
提交回复
热议问题