0

Has anyone successfully gotten plurals to preview in SwiftUI?

Apple uses a .stringsdict file for handling pluralization. However, when attempting to render that string in a preview, for example:

Text("days \(1)", tableName: "Pluralized", bundle: Bundle.module)

The pluralization never seems to work properly when previewed in Xcode. At runtime it works.

esilver
  • 27,713
  • 23
  • 122
  • 168
  • I suspect the problem is that it is not really building the full app, and therefore there is no app bundle from which it can draw. I’ve historically bumped up against analogous limitations when dealing with `@IBDesignable` types in UIKit apps. It would be nice if it handled this more gracefully. – Rob Apr 14 '23 at 19:18

1 Answers1

-1

You have to use lld, not d, as your format character for the number.

This does not work:

    <key>days %d</key>
    <dict>
      <key>NSStringLocalizedFormatKey</key>
      <string>%#@days@ fa</string>
      <key>days</key>
      <dict>
        <key>NSStringFormatSpecTypeKey</key>
        <string>NSStringPluralRuleType</string>
        <key>NSStringFormatValueTypeKey</key>
        <string>d</string>
        <key>one</key>
        <string>%d giorno</string>
        <key>other</key>
        <string>%d giorni</string>
      </dict>
    </dict>

Instead, write your pluralized string in this way:

    <key>days %lld</key>
    <dict>
      <key>NSStringLocalizedFormatKey</key>
      <string>%#@days@ fa</string>
      <key>days</key>
      <dict>
        <key>NSStringFormatSpecTypeKey</key>
        <string>NSStringPluralRuleType</string>
        <key>NSStringFormatValueTypeKey</key>
        <string>lld</string>
        <key>one</key>
        <string>%lld giorno</string>
        <key>other</key>
        <string>%lld giorni</string>
      </dict>
    </dict>
esilver
  • 27,713
  • 23
  • 122
  • 168