AttributedString Localization does not seem to work

I have this code

struct TestAppleSuggestion: View {
   
    @Environment(\.locale) var locale: Locale
 
    var body: some View {
        VStack {
           
            VStack {
                Text("locale: \(locale.identifier)")
                Text(AttributedString(localized: LocalizedStringResource(
                    "welcome",
                    locale: locale
                )))
                Text(AttributedString(localized:
                    "welcome",
                    locale: locale
                ))
            }

        }
    }

}

#Preview {
    TestAppleSuggestion().environment(\.locale, Locale(identifier: "fr-CA"))
}

Heres What I see in SwiftUi Previews

The Localization is working for the LocalizedStringResource but not to the AttributedString.

Why?

AttributedString(localized: "welcome", locale: locale) does return the localized version string for me. Would you mind to share your test project and the detailed steps you used to observe the issue? Thanks.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

is there a secure way for me to send the project to you? or maybe you can share your project? I observed the issue in SwiftUi Previews.

Hello!

You are right: the two locale parameters do different things.

On AttributedString(localized: ) and String(localized: ), as well as String.init(format: ,locale: , args: any CVarArg), the locale parameter influences how interpolated strings are resolved. For example, Foundation adds BiDi control characters when interpolating LTR text into an RTL string, or formats numbers differently. It can not be used to load resources from other locales.

In LocalizedStringResource, the locale parameter influences that, and also tells Foundation to look up the string from the provided locale.

Hope this helps!

AttributedString Localization does not seem to work
 
 
Q