BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

SOLVED: SwiftUI Localization of Strings inside an Array

Forums > SwiftUI

Hi everyone! I'm trying to localize my App and everything worked for now, expect the strings inside an array apparently get not localized. This is the array and the picker:

let days: [String] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

ForEach (days,id: \.self) { day in
                            Text (day)
                                .font (.callout)
                                .fontWeight(.semibold)
                                .frame(maxWidth: .infinity)
                   }

I'm trying to locate it in Spanish and this is the part than doesn't work in the Localizable.strings (Spanish):

"Mon" =  "Lun";
"Tue" = "Mar";
"Wed" = "Mier";
"Thu" = "Jue";
"Fri" = "Vier";
"Sat" = "Sab";
"Sun" =  "Dom";

Any idea? Thanks!

   

I use an extension on String

/**
     A NSLocalizedString for this String where a key has to be added to Localizable.strings file.
     */
    var localized: String {
        return NSLocalizedString(self, comment: "")
    }

    func localized(_ arguments: CVarArg...) -> String {
        return String(format: self.localized, arguments: arguments)
    }

you can use it with day.localized.

   

Perfect, this has solved my problem. Thank you!

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.