1

I'm facing some problems with a SwiftUI NavigationStack and the "Back"-button that appears automatically in the upper left corner of the screen.

Even in a very simple application the Localization of this string only works if a Localizable.strings file for this language exists. All other "Apple generated strings", for example the buttons of access confirmation dialogs (GPS, Notifications, ...) are translated automatically after switching the language in the iOS device. And more interesting, there is no need for a Localization-entry for "Back", the translation is done automatically. Also, no translations is possible.

Are there some hints to handle this behavior?

2
  • How do you want to "handle" this behaviour? Do you want to use your own translations for the button, or do you want iOS to do it automatically like the other "Apple generated strings"? Commented Jun 13, 2023 at 8:50
  • It depends ... In most situations a "translations by iOS" would be great, especially for language I can't provide a translation by my own. But there are other apps where a self defined translation would suite better. But what really amazes me is that Apple has chosen this special solution for the back button, I just don't understand it ... Commented Jun 13, 2023 at 9:34

1 Answer 1

1

As this post says, there is no known way of getting the automatic translation if your app is not localised to that language.

That said, whether or not your app is "localised to that language" is not determined by the Localizable.strings files you have. Technically, you can add as many localisations as you want in your project settings, without localising a single file. Though, of course, I don't recommend doing this:

enter image description here

To use your own translated text for the buttons, you can just make your own custom back button in a way that is very similar to the system one.

Here is an example:

struct ContentView: View {

    var body: some View {
        NavigationStack {
            NavigationLink("Navigate") {
                Destination()
            }
        }
    }
}

struct Destination: View {
    @Environment(\.dismiss) var dismiss
    
    var body: some View {
        Text("This is the destination")
            .navigationBarBackButtonHidden()
            .toolbar {
                ToolbarItem(placement: .navigation) {
                    Button {
                        dismiss()
                    } label: {
                        HStack {
                            Image(systemName: "chevron.left")
                                .flipsForRightToLeftLayoutDirection(true)

                            // this is the localised string key that you will use in Localizable.strings
                            Text("Back")
                        }
                    }
                }
            }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Does this button have the ability to navigate the complete navigation tree? Like the system back button?
@TarikWeiss I’m not sure what you mean by “ability to navigate the complete navigation tree”. Like the system back button, this button only pops one view off of the navigation stack.
You can long press the back button. Then a list with the „path items“ appears. Does this work for this variant too?
@TarikWeiss Oh really? I didn't know that. The button in this answer doesn't do that. I'd imagine it would be pretty simple to implement something similar though. Get a navigation path from the NavigationStack, add a contextMenu to the button, and put the items from in path in the context menu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.