How to localize title and description? (LocalizedStringResource)
Asked Answered
F

2

7

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

Failure answered 14/9, 2022 at 12:8 Comment(0)
K
2

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)")
Kaolin answered 18/12, 2022 at 23:35 Comment(1)
Hi @iTarek, I tried it and it works. I guess my problem was that I used localizable.strings and not Localizable.strings as the file name. Thank you very much :)Failure
N
0

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)
Nerland answered 22/9, 2022 at 17:12 Comment(6)
Thanks for the answer, but this does not work for me. First of all I think you meant "LocalizedStringResource" and not "LocalizableStringResource" ;) When I tried it your way there is no localization happening. I tried it with different localizable.strings files. When I use the default parameter it only uses the default string. Maybe this is a bug in iOS 16 and swift?Failure
static var title : LocalizedStringResource = LocalizedStringResource("add_mem_sc_title", defaultValue: "Test", table: "LocalizableShortcuts.strings", locale: Locale.current, comment: "Create a new Memory") This is what I have for the localization of my shortcut, but it only shows "Test" in the shortcuts app, even if I have maintained the "LocalizableShortcuts.strings" file.Failure
Ah yeah, thanks, I typed the wrong thing in the one place where I didn't copy from actual code ¯_(ツ)_/¯Nerland
To be clear -- you're seeing that in the Shortcuts app? Are you able to get your LocalizedStringResource to localize correctly if you use it in a SwiftUI view? One other tidbit on that: I haven't been able to get LocalizedStringResource's locale: param to behave as advertised, in that setting locale there doesn't seem to change the localization you get. But setting locale on the view (.environment(\.locale, .init(identifier: "fr")) or whatever) does seem to work.Nerland
Seems like what you're doing is supposed to work - definitely seems like it might be a bug in iOS 16!Nerland
This worked for me by just using the key and defaultValue. Since my translations are in Localizable.strings I did not provide a value for table because Localizable.string is default. tatic var title : LocalizedStringResource = LocalizedStringResource("add_mem_sc_title", defaultValue: "Test")Picky

© 2022 - 2024 — McMap. All rights reserved.