I am engaged on integrating RTL (right-to-left) and LTR (left-to-right) layouts into my software, and all the pieces appears to be functioning correctly. Nevertheless, I’ve encountered a difficulty: once I initially choose ‘en’, my app adopts the LTR structure as anticipated. However once I swap the language to ‘Arabic’, the semanticContentAttribute look adjustments completely. Nevertheless, on the identical time, UIApplication.shared.userInterfaceLayoutDirection == .rightToLeft
returns false
, which is right. After I swap the language, the widgets do not shift their structure, such because the placeholder of a UITextField
.
What could possibly be inflicting this conduct?
//
// SceneDelegate.swift
// ____
//
// Created by XYZ on 10/02/2024.
//
import UIKit
import Floaty
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
var scene: UIWindowScene?
var mainNavigationController: UINavigationController!
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, choices connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
self.scene = scene
SharedManager.supervisor.updateLanguage()
NotificationCenter.default.addObserver(self, selector: #selector(languageReset(notification:)), identify: .languageReset, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(changeRootVc(notification:)), identify: .changeVc, object: nil)
}
@objc func changeRootVc(notification: NSNotification){
if let scene {
setupLaunchScreen(scene: scene, vc: MainTabbarViewController.instantiate(for: .Primary))
}
}
@objc func languageReset(notification: NSNotification) {
if let scene {
if notification.userInfo?["isLanguageAlreadySetUp"] as? Bool ?? false {
setupLaunchScreen(scene: scene, vc: MainTabbarViewController.instantiate(for: .Primary))
} else {
setupLaunchScreen(scene: scene, vc: LanguageSelectionViewController.instantiate(for: .Primary))
}
}
}
func setupLaunchScreen(scene: UIWindowScene, vc: UIViewController){
SharedManager.supervisor.updateLanguage()
if SharedManager.supervisor.isArabicLanguage {
Floaty.international.rtlMode = true
UIView.look().semanticContentAttribute = .forceRightToLeft
} else {
Floaty.international.rtlMode = false
UIView.look().semanticContentAttribute = .forceLeftToRight
}
self.window = UIWindow(windowScene: scene)
mainNavigationController = UINavigationController(rootViewController: vc)
mainNavigationController.setNavigationBarHidden(true, animated: false)
UIView.transition(with: self.window!, length: 0.5, choices: .transitionCrossDissolve, animations: {
self.window?.rootViewController = self.mainNavigationController
}, completion: nil)
self.window?.makeKeyAndVisible()
}
}
The Language Handler
import Basis
enum Language: String, CaseIterable {
case english = "en"
case arabic = "ar"
static func enumFor(_ rawValue: RawValue) -> Language? {
return Language(rawValue: rawValue)
}
}
class SharedManager : NSObject {
static let supervisor: SharedManager = SharedManager()
var langDictionary: Dictionary<String, String>!
non-public override init() {}
var isArabicLanguage: Bool {
return SharedManager.supervisor.language == .arabic
}
var isLangAlreadySet: Bool {
return UserDefaults.normal.string(forKey: "language") != nil
}
func updateLanguage() {
guard let currentLanguage = UserDefaults.normal.string(forKey: "language"),
let language = Language.enumFor(currentLanguage) else {
return
}
self.language = language
self.checkLanguageSupport()
}
func checkLanguageSupport() {
guard let path = Bundle.foremost.path(forResource: SharedManager.supervisor.language?.rawValue, ofType: "json"),
let knowledge = strive? Information(contentsOf: URL(fileURLWithPath: path)) else {
print("Language file not discovered")
return
}
do {
langDictionary = strive JSONSerialization.jsonObject(with: knowledge, choices: []) as? Dictionary<String, String>
} catch {
print("Error parsing language file")
}
}
var language: Language? {
didSet {
guard let language = language else { return }
SharedManager.supervisor.checkLanguageSupport()
UserDefaults.normal.set([language.rawValue], forKey: "AppleLanguages")
UserDefaults.normal.set(language.rawValue, forKey: "language")
}
}
}