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?

PDPS
  • 31
  • 3
  • 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"? – Sweeper Jun 13 '23 at 08: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 ... – PDPS Jun 13 '23 at 09:34

1 Answers1

0

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")
                        }
                    }
                }
            }
    }
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313