I need to make a contemporary language change, however I don’t know how you can restart the applying, or relatively I do know, however for me it really works with an error and never the way in which I would like, in different functions I noticed that the language is up to date with out leaving the applying, the slider is simply spinning and that’s it, the language changed How you can obtain this in SwiftUI?
That is how I run the applying:
import SwiftUI
@predominant
struct MyApp_UkraineApp: App {
@StateObject var appSettings = AppSettings()
var physique: some Scene {
WindowGroup {
MainMenuView()
.environmentObject(appSettings)
}
}
}
That is how I alter languages:
import SwiftUI
struct MainMenuView: View {
@EnvironmentObject non-public var appSettings: AppSettings
var physique: some View {
HStack {
Button("Ukraine") {
Language.chosen = .ukraine
resetApp()
}
Button("English") {
Language.chosen = .english
resetApp()
}
}
func resetApp() {
if let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
if let windowSceneDelegate = scene.delegate as? UIWindowSceneDelegate {
windowSceneDelegate.window??.rootViewController = UIHostingController(rootView: MainMenuView())
}
}
}
}
}
enum Language: String, CaseIterable {
case english, ukraine
var code: String {
change self {
case .english: return "en"
case .ukraine: return "uk"
}
}
static var chosen: Language {
set {
UserDefaults.customary.set([newValue.code], forKey: "AppleLanguages")
UserDefaults.customary.set(newValue.rawValue, forKey: "language")
}
get {
return Language(rawValue: UserDefaults.customary.string(forKey: "language") ?? "") ?? .english
}
}
static func switchLanguageBetweenEnglishAndGerman() {
chosen = chosen == .english ? .ukraine : .english
}
}
I get this error, I do know why it seems, however I don’t know how you can repair it:
Thread 1: Deadly error: No ObservableObject of kind AppSettings discovered. A View.environmentObject(_:) for AppSettings could also be lacking as an ancestor of this view.
However the primary query is how to ensure that there’s a fashionable language replace with out restarting the applying?