Change language and layout direction in SwiftUI

Viewed 1059

I'm using SwiftUI, and I know that the easiest way of localization is to add languages, and leave it to the OS to determine which language to use. But in my case, I need to determine a specific default language for the app, because:

  1. The app MUST be in Arabic (RTL), and the other choice could be English (LTR).
  2. In my country, many mobile (and mac) users tend to use English as the language of their devices interface.

The perfect solution is to make user able to switch between Arabic and English after installing the app (on first use), or in Settings, like the following example:

enter image description here

I tried to change locale and layoutDirection of the Content View, but it doesn't change the app layout to RTL:

import SwiftUI
@main
struct MyApp: App {
    var body: some Scene {
        DocumentGroup(newDocument: MyDocument()) { file in
            ContentView(document: file.$document)
                .environment(\.locale, Locale(identifier: "ar"))
                .environment(\.layoutDirection, .rightToLeft)
        }
    }
}

An alternative solution that would also work for me (if there is no perfect way to answer the above problem) is to totally remove the English (and Base) localization, and stick to only Arabic (RTL) by default, how?

1 Answers

I think change app language from product / scheme / edit / then set the app language to arabic may help but this may lead you to another error when you tries to change the language to English

Related