How to deal with interpolated LocalizedStringKey in SwiftUI

Viewed 1812

I'm literally pulling my hair out trying to translate my SwiftUI app for iOS and I'm facing a wall when I need to translate a interpolated String displaying the score of the user.

I already tried some String extensions but it seems not to work correctly. For example, I have :

String Extension

extension String {

    func localized(withComment comment: String? = nil) -> String {
        return NSLocalizedString(self, comment: comment ?? "")
    }

}

Localized String in Localizable.strings

"Score" = "Perf";

The score View in ContentView.swift

ScoreCard("\("Score".localized()): \(quizManager.score)/\(quizManager.numberOfQuestions)")

which gets me to this:

enter image description here

I searched on the web and found nothing working about string interpolation for localizable strings.

Does anyone has a solution for this please?

1 Answers

Use LocalizedStringKey in your content View

LocalizedStringKey("Score \(quizManager.score)/\(quizManager.numberOfQuestions)")
Related