SwiftUI Localization

Viewed 260

I am facing localisation issue in SwiftUI. Issue is happening when translation has placeholder. I am getting error "Instance method 'appendInterpolation' requires that 'LocalizedStringKey' conform to '_FormatSpecifiable'"

Code

struct Translation {
    struct school{
        static var location: LocalizedStringKey {
            return "schoolLocation %@"
        }
     }
}

Translation file

"schoolLocation %@" = "My school location is %@";

SwiftUI view

var location = "Some Name"
.navigationBarTitle("\(Translation.school.location) \(location)") 

Please help me if i am doing something wrong.

1 Answers

What you are doing is you are giving back an already interpolated string with %@ to an interpolated string. So the string that you are generating looks like this: "schoolLocation %@ Some Name". You can do it this way:

struct Translation {
    struct school{
        static func location(name: String): LocalizedStringKey {
            return "schoolLocation \(name)"
        }
     }
}

And then you can use your translation like this:

var location = "Some Name"
.navigationBarTitle(Translation.school.location(name: location))
Related