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?

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

Can you explain from which? I do not see what is the label for onDelete.
Accepted Answer
I mean this, if you swipe right, then stands there "Delete". How can I change this?

I mean this, if you swipe right, then stands there "Delete". How can I change this?

You mean you want to localize the swipe action label "Delete"?
Such a system resource should be automatically localized with Language settings properly set. Have you tried?

EDIT
Seems simply setting the Language does not change "Delete".
Have you succeeded?


By the way, you marked your own reply as SOLVED, this means your issue is solved. Have you find the solution?
I have added Japanese (ja) to Localizations of PROJECT./Info.

And set the Language of the simulator to 日本語(Japanese), then the swipe action for "Delete" is shown as "削除" (meaning "Delete" in Japanese).

I believe this may work for your project using Deutsch, assuming the base development language of your project is English (Xcode default).
@OOPer:
That I marked my own question to "Resolved" was a provided, but I don't know how can I undo this.


How can I change the language settings in Xcode?

but I don't know how can I undo this.

Unfortunately this site does not provide us to unmark SOLVED. You need to make extra care about that. (Bad UI design!)

How can I change the language settings in Xcode?

Not in Xcode. In simulators (or actual devices). Open Settings app and go General > Languages and Regions.
I have done it, but it doesn't works. :(

I have done it, but it doesn't works. :(

What is it? Please clarify what you have done.

What is it? Please clarify what you have done.

I changed the Language to German, but there stands still "Delete".


I changed the Language to German

Seems you are missing one of my replies.

I have added Japanese (ja) to Localizations of PROJECT./Info.

And set the Language of the simulator to 日本語(Japanese), then the swipe action for "Delete" is shown as "削除" (meaning "Delete" in Japanese).

I believe this may work for your project using Deutsch, assuming the base development language of your project is English (Xcode default). 

Yes, but where can I found the PROJECT./Info, or is it the file info.plist?

Yes, but where can I found the PROJECT./Info, or is it the file info.plist?

Select the project icon in the Project navigator.

You can find PROJECT and TARGET in the left hand pane of editing area.

Choose the project icon just below the title PROJECT.

You can find tabs showing Info, Build Settings and Swift Packages.

Choose Info.
I did all what you said and I wroted the Project completley in English.
It doesn´t works! What could I make wrong?

What could I make wrong?

Sorry, hard to say without checking your project and the simulators you used. I have done the same things using German (Deutsch) and the Delete action is shown as Löschen.

Better try with a brand-new project and see what happens.
Tested @OOPer answer, and it works well. However, let say you are
building a language App and need to change "Delete" for a selected language dynamically.

I've tried the following test code, but that does not work. How can the language be change in code?
Code Block
struct ContentView: View {
@State var items = ["item 1","item 2","item 3"]
var body: some View {
List() {
ForEach(0 ..< items.count) {
Text(items[$0])
}.onDelete(perform: deleteTasks)
}
.environment(\.locale, Locale(identifier: "ja")) // <-- try to change the language
}
}
func deleteTasks(offsets: IndexSet) { }




onDelete change label
 
 
Q