'SwiftUI searchbar not work when change Language
I have the following problem, if I change the language to English, the searchbar still works in German although I automatically translate the texts with LocalizedString.
does anyone have a solution what could be wrong?
Thanks :)
import Foundation
struct Search: View {
let library: Library
@State private var searchText = ""
var body: some View {
NavigationView {
List {
ForEach(dataVar) { data in
SearchViewPresent(library: data)
}
}.navigationTitle(Text("Entdecke"))
}
.searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always), prompt: "") {}
}
var dataVar: [Library] {
if searchText.isEmpty {
return libraryDataMusik
} else {
return libraryDataMusik.filter { ($0.title.lowercased().contains(searchText.lowercased())) }
}
}
}
struct Search_Previews: PreviewProvider {
static var previews: some View {
Group {
Search(library: libraryDataKlangwelt[0])
.environment(\.locale, .init(identifier: "en"))
.preferredColorScheme(.light)
Search(library: libraryDataKlangwelt[0])
.preferredColorScheme(.light)
.environment(\.locale, .init(identifier: "de"))
}
}
}
Here is my libraryDataMusik. it's about the fact that the text doesn't work with the searchbar when it's translated to english.
let libraryDataMusik = [ // Section 2 Music
Library(title: "Kaminzauber", description: "Entspannung am Kamin", image: "kamin", songs:
[Songs(name: "Song1", image: "", time: ""),
Songs(name: "Song2", image: "", time: ""),
Songs(name: "Song3", image: "", time: ""),
Songs(name: "Song4", image: "", time: ""),
Songs(name: "Song5", image: "", time: ""),
Songs(name: "Song6", image: "", time: ""),
Songs(name: "Song7", image: "", time: ""),
Songs(name: "Song8", image: "", time: ""),
Songs(name: "Song9", image: "", time: ""),
Songs(name: "Song10", image: "", time: "")]),
Library(title: "Menschen", description: "Musik die berührt", image: "menschen", songs:
[Songs(name: "Song1", image: "", time: ""),
Songs(name: "Song2", image: "", time: ""),
Songs(name: "Song3", image: "", time: ""),
Songs(name: "Song4", image: "", time: ""),
Songs(name: "Song5", image: "", time: ""),
Songs(name: "Song6", image: "", time: ""),
Songs(name: "Song7", image: "", time: ""),
Songs(name: "Song8", image: "", time: ""),
Songs(name: "Song9", image: "", time: ""),
Songs(name: "Song10", image: "", time: "")]),
Library(title: "Klänge", description: "Sounds aus dem Revier", image: "glocken", songs:
[Songs(name: "Song1", image: "", time: ""),
Songs(name: "Song2", image: "", time: ""),
Songs(name: "Song3", image: "", time: ""),
Songs(name: "Song4", image: "", time: ""),
Songs(name: "Song5", image: "", time: ""),
Songs(name: "Song6", image: "", time: ""),
Songs(name: "Song7", image: "", time: ""),
Songs(name: "Song8", image: "", time: ""),
Songs(name: "Song9", image: "", time: ""),
Songs(name: "Song10", image: "", time: "")]),
]
My TranslateCode Text
"Kaminzauber" = "Fireplace magic";
"Entspannung am Kamin" = "Relaxation by the fireplace";
"Babysounds" = "Babysounds";
"Einschlafhilfe für dein Baby" = "Sleep aid for your baby";
"Natur" = "Nature";
"Geräusche aus der Natur" = "Sounds from nature";
"Piano" = "Piano";
"Spüre die Tasten" = "Feel the keys";
"Binaural Sounds" = "Binaural Sounds";
"Verstehe die Illusion" = "Understand the illusion";
"Einstellungen" = "Settings";
My Library/Struct
struct Library: Identifiable {
var id = UUID()
var title = LocalizedStringKey("")
var description: String
var image: String
var songs: [Songs]
//var description = LocalizedStringKey("")
}
struct Songs: Hashable {
var id = UUID()
var name: String
var image: String
var time: String
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


