Close Menu
  • Home
  • AI
  • Big Data
  • Cloud Computing
  • iOS Development
  • IoT
  • IT/ Cybersecurity
  • Tech
    • Nanotechnology
    • Green Technology
    • Apple
    • Software Development
    • Software Engineering

Subscribe to Updates

Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

    What's Hot

    Working with @Generable and @Guide in Foundation Models

    July 18, 2025

    Navigating the labyrinth of forks

    July 18, 2025

    OpenAI unveils ‘ChatGPT agent’ that gives ChatGPT its own computer to autonomously use your email and web apps, download and create files for you

    July 18, 2025
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    Big Tee Tech Hub
    • Home
    • AI
    • Big Data
    • Cloud Computing
    • iOS Development
    • IoT
    • IT/ Cybersecurity
    • Tech
      • Nanotechnology
      • Green Technology
      • Apple
      • Software Development
      • Software Engineering
    Big Tee Tech Hub
    Home»iOS Development»In-App Language Switch in iOS with SwiftUI
    iOS Development

    In-App Language Switch in iOS with SwiftUI

    big tee tech hubBy big tee tech hubJune 11, 2025005 Mins Read
    Share Facebook Twitter Pinterest Copy Link LinkedIn Tumblr Email Telegram WhatsApp
    Follow Us
    Google News Flipboard
    In-App Language Switch in iOS with SwiftUI
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    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

    InApp iOS Language SwiftUI Switch
    Follow on Google News Follow on Flipboard
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Copy Link
    tonirufai
    big tee tech hub
    • Website

    Related Posts

    Working with @Generable and @Guide in Foundation Models

    July 18, 2025

    Deep dive into Swift frameworks

    July 17, 2025

    This “smart coach” helps LLMs switch between text and code | MIT News

    July 17, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Editors Picks

    Working with @Generable and @Guide in Foundation Models

    July 18, 2025

    Navigating the labyrinth of forks

    July 18, 2025

    OpenAI unveils ‘ChatGPT agent’ that gives ChatGPT its own computer to autonomously use your email and web apps, download and create files for you

    July 18, 2025

    Big milestone for the future of quantum computing.

    July 18, 2025
    Advertisement
    About Us
    About Us

    Welcome To big tee tech hub. Big tee tech hub is a Professional seo tools Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of seo tools, with a focus on dependability and tools. We’re working to turn our passion for seo tools into a booming online website. We hope you enjoy our seo tools as much as we enjoy offering them to you.

    Don't Miss!

    Working with @Generable and @Guide in Foundation Models

    July 18, 2025

    Navigating the labyrinth of forks

    July 18, 2025

    Subscribe to Updates

    Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

      • About Us
      • Contact Us
      • Disclaimer
      • Privacy Policy
      • Terms and Conditions
      © 2025 bigteetechhub.All Right Reserved

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