UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Multi Language SwiftUI App

Forums > SwiftUI

Hi All, I hope someone can come up with a solution. I know it is possible and actually very easy to have a multilanguage app which uses system or system-configured language. Is it however possible to have some kind of setting inside the application which would enable user to select the application language in some kind of menu?

1      

First, you must create all the localizable files with the languages you want your app to handle.

Then, you can get the current language on device on first index in UserDefaults.standard.stringArray(forKey: "AppleLanguages"), then wherever you want you can set in your app the new language by setting "AppleLanguages" like

UserDefaults.standard.set(["es-MX"], forKey: "AppleLanguages")
UserDefaults.standard.synchornize()

1      

Once you have the localizable files of all the languages it supports

Why do you not just have a Button for them to change the laugauge of the app in Settings.

Button("Change Language") {
    if let url = URL(string: UIApplication.openSettingsURLString) {
        UIApplication.shared.open(url)
    }
}

This will then open the App Settings and in there is a PREFERRED LANGUAGE section which will set it for that app only. You will get all the other translation etc for free EG(Back button in NavigationView). App Settings

1      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.