Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

ios - Change language and layout direction in SwiftUI

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?

question from:https://stackoverflow.com/questions/65877671/change-language-and-layout-direction-in-swiftui

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...