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:
- The app MUST be in Arabic (RTL), and the other choice could be English (LTR).
- 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:
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?
question from:
https://stackoverflow.com/questions/65877671/change-language-and-layout-direction-in-swiftui 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…