0
votes

As per Apple documentation, this should work:

If you initialize a text view with a string variable rather than a string literal, the view triggers the init(:) initializer instead, because it assumes that you don’t want localization in that case. If you do want to localize the value stored in a string variable, you can choose to call the init(:tableName:bundle:comment:) initializer by first creating a LocalizedStringKey instance from the string variable:

Text(LocalizedStringKey(someString)) // Localizes the contents of someString.

https://developer.apple.com/documentation/swiftui/text/init(_:tablename:bundle:comment:)

This is also recommended here: https://www.ibabbleon.com/swiftui_localization_tutorial.html

However, at least in my case, it does not. In the following example, only "Some content 1" value is exported for localization.

struct ContentView: View {
    let text = "Some content 2"
    var body: some View {
        Text("Some content 1", comment: "This text is exported for localization, as expected.")
            .padding()
        Text(LocalizedStringKey(text), comment: "This text is not exported for localization, which is not expected behaviour.")
            .padding()
    }
}

In the app settings, "Use compiler to extract swift strings" is set to Yes.

What am I missing?

It says "If you do want to localize" - it does not say "if you want to export for localization" - 1st is a framework feature, 2nd belongs to Xcode behavior. Works as documented. - Asperi