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?

bennyyy999
  • 80
  • 1
  • 5
  • I'm not using SwiftUI, but could this be the same issue: https://stackoverflow.com/questions/71349719/can-i-pass-a-string-containing-markdown-to-custom-view ? – Larme Apr 06 '22 at 08:22
  • Why would you expect them to be localized? How are you localizing a string that's not in an array? – Shadowrun Apr 06 '22 at 09: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 – bennyyy999 Apr 06 '22 at 11:03
  • This does not work for String variables. You need to localize the variable when putting it in the text – Ptit Xav Apr 06 '22 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] – Shadowrun Apr 06 '22 at 11:10
  • Look at [this](https://stackoverflow.com/q/62042521/13944750) – Ptit Xav Apr 06 '22 at 11:10

3 Answers3

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))
Ptit Xav
  • 3,006
  • 2
  • 6
  • 15
0

This works for me:

Picker("Select Category", selection: $transactionTypeString) {
    ForEach(transactionTypes, id:\.self) {
        Text(LocalizedStringKey($0))
    }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
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.

lorem ipsum
  • 21,175
  • 5
  • 24
  • 48