AppIntents and String catalog: how can we support both singular and plural forms for TypeDisplayRepresentation (used by DeleteIntent in the Shortcuts app for example)

Hello,

I’m implementing the AppIntents framework in my app. I want to translate the TypeDisplayRepresentation that is used in the Shortcuts app UI like in a DeleteIntent (see my feedback about this: FB23451186 for more context).

In the “Accelerating app interactions with App Intents” sample code we can see that this is done using a .stringsdict file, as follows for the “Trail” key (singular and plural):

<key>Trail</key>
        <dict>
            <key>NSStringLocalizedFormatKey</key>
            <string>%#@VARIABLE@</string>
            <key>VARIABLE</key>
            <dict>
                <key>NSStringFormatSpecTypeKey</key>
                <string>NSStringPluralRuleType</string>
                <key>one</key>
                <string>Trail</string>
                <key>other</key>
                <string>Trails</string>
            </dict>
        </dict>

I want to use a String catalog instead of a .stringsdict file because all my strings are in a String catalog.

I tried to migrate the AppIntents.stringsdict file manually but it failed with an error:

“An error occurred when migrating AppIntentsSampleApp/Resources/AppIntents.stringsdict: This stringsdict cannot be migrated: Missing required key 'NSStringFormatValueTypeKey' inside 'Trail' -> 'VARIABLE’”

So I manually added a NSStringFormatValueTypeKey like this in the .stringsdict file:

<key>Trail</key>
        <dict>
            <key>NSStringLocalizedFormatKey</key>
            <string>%#@VARIABLE@</string>
            <key>VARIABLE</key>
            <dict>
                <key>NSStringFormatSpecTypeKey</key>
                <string>NSStringPluralRuleType</string>
                <key>NSStringFormatValueTypeKey</key>
                <string>lld</string>
                <key>one</key>
                <string>Trail</string>
                <key>other</key>
                <string>Trails</string>
            </dict>
        </dict>

And then I’ve been able to migrate the .stringsdict file into a String catalog.

The string catalog looks like this after migration:

"Trail" : {
      "extractionState" : "stale",
      "localizations" : {
        "en" : {
          "stringUnit" : {
            "state" : "translated",
            "value" : "%#@VARIABLE@"
          },
          "substitutions" : {
            "VARIABLE" : {
              "formatSpecifier" : "lld",
              "variations" : {
                "plural" : {
                  "one" : {
                    "stringUnit" : {
                      "state" : "translated",
                      "value" : "Trail (Catalog)"
                    }
                  },
                  "other" : {
                    "stringUnit" : {
                      "state" : "translated",
                      "value" : "Trails (Catalog)"
                    }
                  }
                }
              }
            }
          }
        }
      }

This works, which is nice.

But I tried to reproduce the same result by having a %#@VARIABLE@ in my LocalizedStringResource defaultValue like this:

TypeDisplayRepresentation(
            name: LocalizedStringResource(
                "Flower",
                defaultValue: "%#@VARIABLE@",
                table: "AppIntents"
            ),
            numericFormat: LocalizedStringResource(
                "\(placeholder: .int) flower",
                table: "AppIntents"
            )
        )

But the String catalog doesn’t support that apparently, I can’t get a “substitution” object in my catalog, so I have to manually do it using the source code which is not ideal and painful.

Is there a way to support this kind of substitution with no actual plural token in the string as we can see for the %#@VARIABLE@ for Trail?

Thank you,

Regards,

Axel

AppIntents and String catalog: how can we support both singular and plural forms for TypeDisplayRepresentation (used by DeleteIntent in the Shortcuts app for example)
 
 
Q