更改语言时 SwiftUI 搜索栏不起作用

发布于 2025-01-11 19:26:02 字数 5277 浏览 0 评论 0原文

我遇到以下问题,如果我将语言更改为英语,尽管我使用 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"))
        }
    }
}

enter image description hereenter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文