Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

How I can localize an array ?

Hello,

I am trying to localize my app in other languages. Most of the text are automatically extracted by Xcode when creating a string catalog and running my app.

However I realized few texts aren't extracted, hence I find a solution for most of them by adding

For example I have some declared variable as

var title: String = "Continue"

Hence for them to be trigger I changed the String by LocalizedStringResource which gives

var title: LocalizedStringResource = "Continue"

But I still have an issue with some variables declared as an array with this for example

@State private var genderOptions = ["Male", "Female", "Not Disclosed"]

I tried many things however I don't success to make these arrays to be translated as the word here "Male", "Female" and "Not Disclosed".

I have more array like that in my code and I am trying to find a solution for them to be extracted and be able to be localized

Few things I tried that doesn't worked

@State private var genderOptions : LocalizedStringResource = ["Male", "Female", "Not Disclosed"]

@State private var genderOptions = [LocalizedStringResource("Male"), LocalizedStringResource("Female"), LocalizedStringResource("Not Disclosed")]

Any idea more than welcome

Thank guys

Answered by Developer Tools Engineer in 841606022

Hello,

You are right that LocalizedStringResource is the preferred way to pass around a localized string.

In your Array example, it really depends on where you intend to use the array. For example, making the type an Array of LocalizedStringResource is valid and each string would get extracted to the String Catalog. However, this may impact the way you use the string in your SwiftUI code.

If these are static options, an alternative could be to use an enum:

enum Gender: Hashable, Identifiable, CaseIterable {
    case male
    case female
    case notDisclosed

    var displayName: LocalizedStringResource {
        switch self {
        case .male: "Male"
        …
        }
    }
}

…

Picker("Gender", selection: $selectedGender) {
    ForEach(Gender.allCases)  { gender in
        Text(gender.displayName)
    }
}

OR it may be that you have a dynamic list that the user is able to change, and these are just the default options. If that is the case, it may be easier to simply use String(localized:), which returns a plain String that is pre-localized.

For example:

@State private var genderOptions = [
    String(localized: "Male"),
    String(localized: "Female"),
    String(localized: "Not Disclosed")
]

You could just as easily store these strings in your model code somewhere using String(localized:) or LocalizedStringResource. To convert a LocalizedStringResource to a String, simply pass it to String(localized:).

One thing to note here is that as soon as the type ends up as a plain String returned from String(localized:), it will be the translated version using the current locale. This differs from passing a LocalizedStringResource directly to SwiftUI since that would use whatever locale is in the SwiftUI environment at the time of view rendering. This is typically the current locale but it can be customized like any other SwiftUI environment property.

Why do you need LocalizedStringResource ?

Did you try with Localizable.strings file as described here: https://www.kodeco.com/books/swiftui-cookbook/v1.0/chapters/1-create-a-localized-string-in-swiftui

Accepted Answer

Hello,

You are right that LocalizedStringResource is the preferred way to pass around a localized string.

In your Array example, it really depends on where you intend to use the array. For example, making the type an Array of LocalizedStringResource is valid and each string would get extracted to the String Catalog. However, this may impact the way you use the string in your SwiftUI code.

If these are static options, an alternative could be to use an enum:

enum Gender: Hashable, Identifiable, CaseIterable {
    case male
    case female
    case notDisclosed

    var displayName: LocalizedStringResource {
        switch self {
        case .male: "Male"
        …
        }
    }
}

…

Picker("Gender", selection: $selectedGender) {
    ForEach(Gender.allCases)  { gender in
        Text(gender.displayName)
    }
}

OR it may be that you have a dynamic list that the user is able to change, and these are just the default options. If that is the case, it may be easier to simply use String(localized:), which returns a plain String that is pre-localized.

For example:

@State private var genderOptions = [
    String(localized: "Male"),
    String(localized: "Female"),
    String(localized: "Not Disclosed")
]

You could just as easily store these strings in your model code somewhere using String(localized:) or LocalizedStringResource. To convert a LocalizedStringResource to a String, simply pass it to String(localized:).

One thing to note here is that as soon as the type ends up as a plain String returned from String(localized:), it will be the translated version using the current locale. This differs from passing a LocalizedStringResource directly to SwiftUI since that would use whatever locale is in the SwiftUI environment at the time of view rendering. This is typically the current locale but it can be customized like any other SwiftUI environment property.

How I can localize an array ?
 
 
Q