2

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.

Rocker
  • 1,269
  • 7
  • 15
  • 1
    Does this answer your question? [How to implement localization in Swift UI](https://stackoverflow.com/questions/58578341/how-to-implement-localization-in-swift-ui) – LuLuGaGa Aug 01 '20 at 17:56
  • No, I have placeholder for dynamic value and for key i am maintaining in structure of LocalizedStringKey. My code is working perfectly for static localization values. but i am getting compilation error for dynamic value. – Rocker Aug 01 '20 at 18:02
  • @Rocker have you tried removing `%@` from the key as it's not necessary? you can have "schoolLocation" = "My school location is %@"; in your translation file. – Mohammad Rahchamani Aug 01 '20 at 20:11
  • Still have getting compilation error "Instance method 'appendInterpolation' requires that 'LocalizedStringKey' conform to '_FormatSpecifiable'" on line .navigationBarTitle("\(Translation.school.location) \(location)") – Rocker Aug 02 '20 at 05:30

1 Answers1

0

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))
Erwin Schens
  • 424
  • 2
  • 9