Search code examples
iosswiftxcodelocalizationswiftui

SwiftUI Localisation not working with @State Strings


I have implemented Localisation in my SwiftUI app. Everything works fine but I'm having issues with localising @State var. Localisation is not working and I'm getting only the keys printed out. Any idea how to fix this issue?

The value of type is already in my Localizable.strings

@State var type: String

var body: some View {
    VStack {
        Text(self.type) // not working
        Text("test") // working
    }
}

Solution

  • You can take convert the string into a NSLocalizedString

    Text(NSLocalizedString(type, comment: ""))
    

    or change the type of type into a LocalizedStringKey

    @State var type: LocalizedStringKey