更改语言时 SwiftUI 搜索栏不起作用
我遇到以下问题,如果我将语言更改为英语,尽管我使用 LocalizedString 自动翻译文本,但搜索栏仍然可以使用德语。
有谁有解决方案可能是错误的吗?
谢谢:)
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"))
}
}
}
这是我的库DataMusik。 这是因为文本翻译成英语后无法在搜索栏上使用。
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: "")]),
]
我的 TranslateCode 文本
"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";
我的库/结构
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
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论