onDelete change label

Hello There,
How can I change the label for onDelete in SwiftUI to e. g. "Löschen"?

Here is my code (just a little bit of ContentView):
Code Block
struct TodoListView: View {
    
    @State var textValue = ""
    @State var descriptionValue = ""
    @State var showAddTaskView: Bool = false
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Task.date, ascending: false)])
    
    private var tasks: FetchedResults<Task>
    
    var body: some View {
        
            NavigationView {
                
                List() {
                    
                    ForEach(tasks) { task in
                        NavigationLink(destination: List { Text(task.beschreibung ?? "Kein Inhalt") }
                        ) {
                            if (task.title == "Unbenannt") {
                                Text(task.title ?? "Unbenannt").italic()
                            } else {
                                Text(task.title ?? "Unbenannt")
                            }
                        }
                        
                    }.onDelete(perform: deleteTasks)
                    
                    
                }
                .listStyle(GroupedListStyle())
                .navigationTitle("Todo-Liste")
                .navigationBarItems( trailing: Button(action: {
                                        showAddTaskView.toggle()
                                    }, label: {
                                        Image(systemName: "plus")
                                            .padding(.leading, 40)
                                            .padding(.bottom, 20)
                                            //.background(Color.yellow)
                                    })
                                    
                
            )}
                .sheet(isPresented: $showAddTaskView, content: {
                    NavigationView {
                        List {
                            TextField("Aufgaben-Name",
                                        text: $textValue)
                            TextEditor(text: $descriptionValue)
                                .padding()
                        }.navigationTitle("Aufgabe")
                        .navigationBarItems(leading: Button(action: {
                            showAddTaskView.toggle()
                            textValue = ""
                            descriptionValue = " "
                        }, label: {
                            Text("Abbrechen").fontWeight(.regular)
                        }),trailing: Button(action: {
                            addTask()
                            showAddTaskView.toggle()
                        }, label: {
                            Text("Hinzufügen")
                        }))
                    
                    }
                
            })
        
    }
    
    
    
    
    private func saveContext() {
        do {
            try viewContext.save()
        } catch {
            let error = error as NSError
            fatalError("Unresolved Error \(error)")
        }
    }
    
    private func deleteTasks(offsets: IndexSet) {
        withAnimation {
            offsets.map { tasks[$0] }.forEach(viewContext.delete)
            saveContext()
            
        }
    }
    
    private func addTask() {
        withAnimation {
            let newTask = Task(context: viewContext)
            if (textValue != "") {
                newTask.title = textValue
            } else {
                newTask.title = "Unbenannt"
            }
            if (descriptionValue != "") {
                newTask.beschreibung = descriptionValue
            } else {
                newTask.beschreibung = "Kein Inhalt"
            }
            newTask.date = Date()
            textValue = ""
            descriptionValue = " "
            
            saveContext()
        }
        
    }
    
}


Thank you for answers.

Answered by 1Spinne in 666175022
I mean this, if you swipe right, then stands there "Delete". How can I change this?
I can share you the project and if it then doesn´t works, I will give up: https://www.icloud.com/iclouddrive/0RTt7FsW3rgq2LZLZeo7kCddA#TodoList

I can share you the project 

Thanks for sharing.

I tried your code and found the swipe action label was shown as Delete even if I set the simulators Language settings to German.

Then I checked your project.
In Localizations, German was actually added, but it shows 0 Files Localized.

Seems your project does not have any resources localizable.
(And, unfortunately, this is the default project setting, when you choose SwiftUI App for Life Cycle.)


Add a new file named Localizable.strings to your project. (It may be empty till you really make your project localizable.)
And localize it including Germany. (You can find a button Localize... in the File Inspector.)
Now it works!!! Thank you so much for your help!!

By the way, you marked your own reply as SOLVED, this means your issue is solved. Have you find the solution?

Now I can say "Yes" :)


Now it works!!! 

Happy to hear that.

Please use the feature of this site properly, if you have next chance. Good luck.

Adding .swipeActions you can add Localized text or icon for swipe to delete case and the default .onDelete still hangs around to be used with EditButton() if you're using with iPhone and Mac app. Can add red trash icon which is universal instead of text.

List {
    ForEach(myArray, id: \.id) { item in
        SomeView(item: item)
            .swipeActions(allowsFullSwipe: false) {
                Button(role: .destructive) {
                    deleteItem(item: item)
                } label: {
                    Label("Delete", systemImage: "trash.fill") // Can also add Localized Text here
                }
            }
    }
    .onDelete(perform: delete)
    .onMove { from, to in
        myArray.move(fromOffsets: from, toOffset: to)
        
    }
}
.toolbar {
    EditButton()
}

private func deleteItem(item: AltimetryItem) {
    if let index = myArray.firstIndex(where: { $0.id == item.id }) {
        withAnimation {
            myArray.remove(at: index)
        }
    }
}

func delete(at offsets: IndexSet) {
    myArray.remove(atOffsets: offsets)
}
onDelete change label
 
 
Q