简体   繁体   中英

How to localize title and description? (LocalizedStringResource)

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:)

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)")

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, eg:

let myString = LocalizableStringResource("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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM