What is best practice to change language(localization) on fly with Modular/TCA ? #1861
Answered
by
mbrandonw
RuslanARmore
asked this question in
Q&A
-
I need to add ability for user to change language without restarting the application. What is the best way to do this? |
Beta Was this translation helpful? Give feedback.
Answered by
mbrandonw
Jan 24, 2023
Replies: 1 comment 2 replies
-
Hi @RuslanARmore, you may not even need to involve TCA in this at all. You can change the language for your entire SwiftUI application using the Paste this into a playground to see how it works: import PlaygroundSupport
import SwiftUI
struct ContentView: View {
@State var locale = Locale.current
@State var isPresented = false
var body: some View {
NavigationStack {
VStack {
Button {
self.isPresented = true
self.locale = Locale(identifier: "en")
} label: {
Text("English")
}
Button {
self.isPresented = true
self.locale = Locale(identifier: "zh")
} label: {
Text("Chinese")
}
}
.alert("😁", isPresented: self.$isPresented) {}
}
.environment(\.locale, self.locale)
}
}
PlaygroundPage.current.setLiveView(ContentView()) |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
RuslanARmore
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @RuslanARmore, you may not even need to involve TCA in this at all. You can change the language for your entire SwiftUI application using the
\.locale
environment value (see here).Paste this into a playground to see how it works: