Runtime Language Change in Swift
Your app is running in single language and you want to convert it into another language at runtime. like your app is running in english and you want to change your application language in french or german etc. You just have to create the strings in your Localizable.string file. and you have to just set the path of that language file which you want to access. set this method in your global class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
var languageBundle : Bundle? func language() { let languageCode = UserDefaults.standard if UserDefaults.standard.value(forKey: "language") != nil { let language = languageCode.string(forKey: "language")! if let path = Bundle.main.path(forResource: language, ofType: "lproj") { languageBundle = Bundle(path: path) } else{ languageBundle = Bundle(path: Bundle.main.path(forResource: "en", ofType: "lproj")!) } } else { languageCode.set("en", forKey: "language") languageCode.synchronize() let language = languageCode.string(forKey: "language")! if let path = Bundle.main.path(forResource: language, ofType: "lproj") { languageBundle = Bundle(path: path) } else{ languageBundle = Bundle(path: Bundle.main.path(forResource: "en", ofType: "lproj")!) } } } |
And the word you you want to change :-
1 |
self.globalObjectHome.languageBundle?.localizedString(forKey: "emailAddress", value: "", table: nil) |
//globalObjectHome is object of global class
// emailAddress = “Email Address”; In your localizable string file