Example:
struct ContentView: View {
@EnvironmentObject var home: HomeGlobal
var body: some View {
MyTextView(title: "Hello")
MyTextView(title: "To English")
.onTapGesture {
home.appLanguage = .english
}
MyTextView(title: "To Hindi")
.onTapGesture {
home.appLanguage = .hindi
}
}
}
struct MyTextView: View {
@EnvironmentObject var home: HomeGlobal
var title: LocalizedStringKey
var body: some View {
Text(title, bundle: home.languageBundle)
}
}
class HomeGlobal: ObservableObject {
// Localization
@Published var appLanguage: AppLanguageType = .english
var languageBundle: Bundle? {
let b = Bundle.main.path(forResource: appLanguage.rawValue, ofType: "lproj")!
return Bundle(path: b)
}
}
enum AppLanguageType: String {
case english = "en"
case hindi = "hi"
}