SwiftUI AppIntents: How to localize title and description? (LocalizedStringResource)

Viewed 46

I am working on my SwiftUI project and added some AppIntents for the Shortcuts app. Currently I am not able to localize the title and description. The title and the description are of type LocalizedStringResource (available from iOS16+).

I tried to localized the following code to German, but I don't know how to do.

struct Add_Memory_Shortcut: AppIntent {
    // The name of the action in Shortcuts
    // TODO: Localization
    static var title : LocalizedStringResource = "Create Memory"
    
    // Description of the action in Shortcuts
    // Category name allows you to group actions - shown when tapping on an app in the Shortcuts library
    static var description: IntentDescription = IntentDescription("Create a new memory", categoryName: "Creating")
}

Any help or link is appreciated. Thanks :)

1 Answers

LocalizedStringResource doesn't have a ton of documentation available, but take a look at https://developer.apple.com/documentation/foundation/localizedstringresource/3988421-init:

init(
    _ keyAndValue: String.LocalizationValue,
    table: String? = nil,
    locale: Locale = .current,
    bundle: LocalizedStringResource.BundleDescription = .main,
    comment: StaticString? = nil
)

For table, it says: "The name of the table containing the key-value pairs. If not provided, nil, or an empty string, this value defaults to Localizable.strings." In my experience that's not currently true, and you may need to explicitly point it to your translations, e.g.:

let myString = LocalizedStringResource("Hello, big world!", table: "Localizable.strings")

If you do that, supply appropriate translations, and set a locale that matches, you will see the localized version presented -- at least within a SwiftUI view:

Text(myString)
Related