2

I'm trying to localize my App in English and German and everything worked for now, expect the strings inside an array apparently get not localized.

I have this array, which holds the options for my Picker:

let sorting = ["Newest First", "Oldest First"]

This is my Picker, which works correctly function wise:

Picker("Sort by", selection: $sortingSelection) {
    ForEach(sorting, id: \.self) {
        Text($0)
            .foregroundColor(.accentColor)
            .font(.footnote)
    }
}
.pickerStyle(MenuPickerStyle())

And this is the correleting Localizable.strings (German):

"Newest First" = "Neueres Zuerst";
"Oldest First" = "Älteres Zuerst";

So I tried just writing it as strings, which is the easiest way to use localization now.

I also tried using the it as a variable, but this doesn't work:

let localizedString1 = "Newest First"
let localizedString2 = "Oldest First"

let sorting = [localizedString1, localizedString2]

I also saw this post:

Swift: Localize an Array of strings

but like the comment on the answer says, is there a way to get some example code? Or is there a better method now?

6
  • I'm not using SwiftUI, but could this be the same issue: stackoverflow.com/questions/71349719/… ? Commented Apr 6, 2022 at 8:22
  • Why would you expect them to be localized? How are you localizing a string that's not in an array? Commented Apr 6, 2022 at 9:38
  • @Shadowrun in SwiftUI you don't need to write anything else anymore than the string itself, it checks if there is a localization or not, so I thought it's gonna be automatic inside an array too Commented Apr 6, 2022 at 11:03
  • This does not work for String variables. You need to localize the variable when putting it in the text Commented Apr 6, 2022 at 11:05
  • Is that because a raw string would be type inferred as LocalizedStringKey to use the init(_ key: LocalizedStringKey, tableName: String? = nil, bundle: Bundle? = nil, comment: StaticString? = nil) initialiser, and your array is [String] not [LocalizedStringKey] Commented Apr 6, 2022 at 11:10

3 Answers 3

3

As stated in Text documentation :

If you intialize a text view with a variable value, the view uses the init(:) initializer, which doesn’t localize the string. However, you can request localization by creating a LocalizedStringKey instance first, which triggers the init(:tableName:bundle:comment:) initializer instead:

// Don't localize a string variable...
Text(writingImplement)


// ...unless you explicitly convert it to a localized string key.
Text(LocalizedStringKey(writingImplement))
Sign up to request clarification or add additional context in comments.

Comments

0

The array defaults to type String, you can specify the type to LocalizedStringKey

let sorting: [LocalizedStringKey] = ["Newest First", "Oldest First"]

You might have to change the selection type too to make them match.

Comments

0

This works for me:

Picker("Select Category", selection: $transactionTypeString) {
    ForEach(transactionTypes, id:\.self) {
        Text(LocalizedStringKey($0))
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.