Reputation: 187
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 localize the following code to German, but I don't know how to do it.
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 :)
Upvotes: 7
Views: 3806
Reputation: 776
I tried everything, only one thing works, add your localization strings in
Localizable.strings
And if you want to localize string with paramter
//Localizable.strings
"hello_user%@" = "Hello %@";
//Your AppIntents
let welcomeMSG = LocalizedStringResource("hello_user\(userName)")
Upvotes: 2
Reputation: 684
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)
Upvotes: 0