skip to Main Content

in project.pbxproj I changed developmentRegion to ar to force arabic language and RTL layout in entire app, everything looks good except Menu view content in this example:

import SwiftUI

struct ContentView: View {
    
    @State private var selectedTime: Int = 0
    
    var body: some View {
        Form {
            Picker("", selection: $selectedTime) {
                Text("صباحاً").tag(0)
                Text("مساءً").tag(1)
            }
            .labelsHidden()
            .pickerStyle(SegmentedPickerStyle())
            Menu {
                Button("السبت", action: {})
                Button("الأحد", action: {})
                Button("الاثنين", action: {})
                Button("الثلاثاء", action: {})
                Button("الاربعاء", action: {})
                Button("الخميس", action: {})
                Button("الجمعة", action: {})
            } label: {
                Label("اليوم", systemImage: "calendar")
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .preferredColorScheme(.dark)
    }
}

the buttons’ labels inside Menu is flipped

Menu button label is flipped

any help is appreciated, thank you

2

Answers


  1. Just add

    init() {
        UserDefaults.standard.set(["ar"], forKey: "AppleLanguages")
    }
    

    Here, is the complete code which works

    import SwiftUI
    
    struct ContentView: View {
        init() {
            UserDefaults.standard.set(["ar"], forKey: "AppleLanguages")
        }
        
        @State private var selectedTime: Int = 0
        
        var body: some View {
            Form {
                Picker("", selection: $selectedTime) {
                    Text("صباحاً").tag(0)
                    Text("مساءً").tag(1)
                }
                .labelsHidden()
                .pickerStyle(SegmentedPickerStyle())
                Menu {
                    Button("السبت", action: {})
                    Button("الأحد", action: {})
                    Button("الاثنين", action: {})
                    Button("الثلاثاء", action: {})
                    Button("الاربعاء", action: {})
                    Button("الخميس", action: {})
                    Button("الجمعة", action: {})
                } label: {
                    Label("اليوم", systemImage: "calendar")
                }
            }
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
                .preferredColorScheme(.dark)
                
        }
    }
    

    Login or Signup to reply.
  2. The only way you can achieve this is change the language through settings and restart the app.

    1. Add any language you need in the project settings settings:
      Project settings

    2. You must add the settings.bundle:

    File -> New -> File -> Settings Bundle
    

    Settings

    Delete useless settings if needed

    1. head to the settings and change it:
      Settings

    Important notes

    • I couldn’t find any workaround this until now (18 August 2021)
    • You should ask the user to change the language if preferes and open the settings for them
    • You can change the language programatically with:
    UserDefaults.standard.set(["ar"], forKey: "AppleLanguages")
    

    BUT!

    • The settings must exist
    • The app must restart after the language change
    • You can force close the app after settings the language for example with the exit() code if the language is not ar

    Development note

    The simulator constantly caches the settings. So you may face issues that prevent you from seeing the current settings. Try test the flow in test projects and implement the result in the actual one.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search