Close Menu
geekfence.comgeekfence.com
    What's Hot

    Customer experience management (CXM) predictions for 2026: How customers, enterprises, technology, and the provider landscape will evolve 

    December 28, 2025

    What to Know About the Cloud and Data Centers in 2026

    December 28, 2025

    Why Enterprise AI Scale Stalls

    December 28, 2025
    Facebook X (Twitter) Instagram
    • About Us
    • Contact Us
    Facebook Instagram
    geekfence.comgeekfence.com
    • Home
    • UK Tech News
    • AI
    • Big Data
    • Cyber Security
      • Cloud Computing
      • iOS Development
    • IoT
    • Mobile
    • Software
      • Software Development
      • Software Engineering
    • Technology
      • Green Technology
      • Nanotechnology
    • Telecom
    geekfence.comgeekfence.com
    Home»iOS Development»In-App Language Switch in iOS with SwiftUI
    iOS Development

    In-App Language Switch in iOS with SwiftUI

    AdminBy AdminDecember 10, 2025No Comments5 Mins Read1 Views
    Facebook Twitter Pinterest LinkedIn Telegram Tumblr Email
    In-App Language Switch in iOS with SwiftUI
    Share
    Facebook Twitter LinkedIn Pinterest Email


    We’ve covered iOS localization in several tutorials, including one that shows how to fully localize an app using String Catalogs. However, these tutorials rely on the system language to determine the app’s language. But what if you want to give users the ability to choose their preferred language, regardless of the system setting? And what if you want the language to update instantly—without restarting the app? That’s exactly what this tutorial will teach you.

    Before we get started, I recommend reviewing the earlier iOS localization tutorial if you’re not familiar with String Catalogs. The demo app used in this tutorial builds on the one from that guide.

    The Demo App

    language-switch-demo-screens.png

    We’re reusing the demo app from our iOS localization tutorial—a simple app with basic UI elements to illustrate localization concepts. In this tutorial, we’ll extend it by adding a Settings screen that lets users select their preferred language. The app will then update the language instantly, with no need to restart.

    Adding App Languages and App Settings

    Before we start building the Setting screen, let’s first add an AppLanguage enum and an AppSetting class to the project. The AppLanguage enum defines the set of languages that your app supports. Here is the code:

    enum AppLanguage: String, CaseIterable, Identifiable {
        case en, fr, jp, ko, zhHans = "zh-Hans", zhHant = "zh-Hant"
        
        var id: String { rawValue }
        
        var displayName: String {
            switch self {
            case .en: return "English"
            case .fr: return "French"
            case .jp: return "Japanese"
            case .ko: return "Korean"
            case .zhHans: return "Simplified Chinese"
            case .zhHant: return "Traditional Chinese"
            }
        }
    }
    

    Each case in the enum corresponds to a specific language, using standard locale identifiers as raw values. For example, .en maps to "en" for English, .fr to "fr" for French, and so on. The displayName computed property provides a user-friendly label for each language. Instead of displaying raw locale codes like “en” or “zh-Hans” in the UI, this property returns readable names such as “English” or “Simplified Chinese.”

    The AppSetting class, which conforms to the ObservableObject protocol, is a simple observable model that stores the user’s selected language. Here is the code:

    class AppSetting: ObservableObject {
        @Published var language: AppLanguage = .en
    }
    

    By default, the language is set to English. Later, when the user selects a different language from the Settings screen, updating this property will cause SwiftUI views that rely on the app’s locale to re-render using the new language.

    Building the Setting Screen

    language-switch-settings.png

    Next, let’s build the Settings screen. It’s a simple interface that displays a list of all the supported languages. Below is the code for implementing the setting view:

    struct SettingView: View {
        
        @Environment(\.dismiss) var dismiss
        
        @EnvironmentObject var appSetting: AppSetting
        
        @State private var selectedLanguage: AppLanguage = .en
        
        var body: some View {
            NavigationStack {
                Form {
                    Section(header: Text("Language")) {
                        ForEach(AppLanguage.allCases) { lang in
                            
                            HStack {
                                Text(lang.displayName)
                                
                                Spacer()
                                
                                if lang == selectedLanguage {
                                    Image(systemName: "checkmark")
                                        .foregroundColor(.primary)
                                }
                                    
                            }
                            .onTapGesture {
                                selectedLanguage = lang
                            }
                        }
                    }
                }
                
                .toolbar {
                    ToolbarItem(placement: .topBarTrailing) {
                        Button("Save") {
                            appSetting.language = selectedLanguage
                            dismiss()
                        }
                    }
    
                    ToolbarItem(placement: .topBarLeading) {
                        Button("Cancel") {
                            dismiss()
                        }
                    }
                }
                .navigationTitle("Settings")
                .navigationBarTitleDisplayMode(.inline)
                
            }
            .onAppear {
                selectedLanguage = appSetting.language
            }
        }
    }
    
    #Preview {
        SettingView()
            .environmentObject(AppSetting())
    }
    

    The view simply lists the available languages as defined in AppLanguage. The currently selected language shows a checkmark next to it. When the user taps “Save,” the selected language is saved to the shared AppSetting object, and the view is dismissed.

    In the main view, we add a Setting button and use the .sheet modifier to display the Setting view.

    struct ContentView: View {
        
        @EnvironmentObject var appSetting: AppSetting
        
        @State private var showSetting: Bool = false
        
        var body: some View {
            VStack {
                
                HStack {
                    Spacer()
                    
                    Button {
                        showSetting.toggle()
                    } label: {
                        Image(systemName: "gear")
                            .font(.system(size: 30))
                            .tint(.primary)
                    }
    
                    
                }
                    
                Text("ProLingo")
                    .font(.system(size: 75, weight: .black, design: .rounded))
                
                Text("Learn programming languages by working on real projects")
                    .font(.headline)
                    .padding(.horizontal)
                  
               .
               .
               .
               .
               .
               .
                
            }
            .padding()
            .sheet(isPresented: $showSetting) {
                SettingView()
                    .environmentObject(appSetting)
            }
    
        }
    }
    
    

    Enabling Real-Time Language Changes

    At this point, tapping the gear button will bring up the Settings view. However, the app doesn’t update its language when the user selects their preferred language. To implement dynamic language switching, we have to attach the .environment modifier to ContentView and update the locale to match the user’s selection like this:

    VStack {
       ...
    }
    .environment(\.locale, Locale(identifier: appSetting.language.id))
    

    This line of code injects a custom Locale into the SwiftUI environment. The \.locale key controls which language and region SwiftUI uses for localizable views like Text. The locale is set to match the language the user selected in settings.

    The app can now update its language on the fly. For example, open the Settings view and select Traditional Chinese. After saving your selection and returning to the main view, you’ll see the UI instantly updated to display all text in Traditional Chinese.

    language-switch-tc.png

    Using LocalizedStringKey

    You may notice a bug in the app. After changing the language to Traditional Chinese (or other languages) and reopening the Settings view, the language names still display in English.

    language-switch-settings-bug.png

    Let’s take a look at the code that handles the display of language name:

    Text(lang.displayName)
    

    You may wonder why the Text view doesn’t handle the localization automatically. In this case, SwiftUI treats lang.displayName as a plain text, which means no automatic localization happens, even if the string matches a key in the String Catalog file. To make the localization work, you need to convert the String to a LocalizedStringKey like this:

    Text(LocalizedStringKey(lang.displayName))
    

    Using LocalizedStringKey triggers the localization lookup process. When you run the app again, you’ll see the language names in the Settings view displayed in your chosen language.

    language-switch-setting-tc.png

    Summary

    In this tutorial, you learned how to implement in-app language switching in iOS using SwiftUI, allowing users to change languages without restarting the app. We explored how to create a Settings screen for language selection, enabled real-time localization updates, and learned the importance of using LocalizedStringKey for proper string localization.

    The code and concepts presented here provide a foundation for implementing language switching in your own iOS apps. Feel free to adapt this approach for your own iOS apps that require multi-language support.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

    Related Posts

    SwiftText | Cocoanetics

    December 28, 2025

    Uniquely identifying views – The.Swift.Dev.

    December 27, 2025

    Unable to upload my app with Transporter. Some kind of version mismatch? [duplicate]

    December 26, 2025

    Experimenting with Live Activities – Ole Begemann

    December 25, 2025

    Announcing Mastering SwiftUI for iOS 18 and Xcode 16

    December 24, 2025

    Grouping Liquid Glass components using glassEffectUnion on iOS 26 – Donny Wals

    December 22, 2025
    Top Posts

    Understanding U-Net Architecture in Deep Learning

    November 25, 20258 Views

    Microsoft 365 Copilot now enables you to build apps and workflows

    October 29, 20258 Views

    Here’s the latest company planning for gene-edited babies

    November 2, 20257 Views
    Don't Miss

    Customer experience management (CXM) predictions for 2026: How customers, enterprises, technology, and the provider landscape will evolve 

    December 28, 2025

    After laying out our bold CXM predictions for 2025 and then assessing how those bets played out…

    What to Know About the Cloud and Data Centers in 2026

    December 28, 2025

    Why Enterprise AI Scale Stalls

    December 28, 2025

    New serverless customization in Amazon SageMaker AI accelerates model fine-tuning

    December 28, 2025
    Stay In Touch
    • Facebook
    • Instagram
    About Us

    At GeekFence, we are a team of tech-enthusiasts, industry watchers and content creators who believe that technology isn’t just about gadgets—it’s about how innovation transforms our lives, work and society. We’ve come together to build a place where readers, thinkers and industry insiders can converge to explore what’s next in tech.

    Our Picks

    Customer experience management (CXM) predictions for 2026: How customers, enterprises, technology, and the provider landscape will evolve 

    December 28, 2025

    What to Know About the Cloud and Data Centers in 2026

    December 28, 2025

    Subscribe to Updates

    Please enable JavaScript in your browser to complete this form.
    Loading
    • About Us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • Terms and Conditions
    © 2025 Geekfence.All Rigt Reserved.

    Type above and press Enter to search. Press Esc to cancel.