Change app language using button programmatically
Use localization
to change the language of the application without restart.
- Go to project -> select your project in the document outline -> Add the new language.
- Add
Localized.strings
files to your project.
- Add the strings which are used for localization in
Localizable.strings(XXX)
For English "hello" = "Hello World";
For Hindi "hello" = "नमस्ते दुनिया";
Code:
import UIKit
extension String {
var localized: String {
let lang = currentLanguage()
let path = Bundle.main.path(forResource: lang, ofType: "lproj")
let bundle = Bundle(path: path!)
return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
}
//Remove here and add these in utilities class
func saveLanguage(_ lang: String) {
UserDefaults.standard.set(lang, forKey: "Locale")
UserDefaults.standard.synchronize()
}
func currentLanguage() -> String {
return UserDefaults.standard.string(forKey: "Locale") ?? ""
}
}
enum Language: String {
case english = "English"
case hindi = "हिंदी"
}
class ViewController: UIViewController {
var language = Language.english
override func viewDidLoad() {
super.viewDidLoad()
//Initial Setup
String().saveLanguage("en")
languageLabel.text = "hello".localized
languageButton.setTitle(language.rawValue, for: .normal)
}
func updateLanguage() {
if language == .english {
String().saveLanguage("hi")
language = .hindi
} else {
String().saveLanguage("en")
language = .english
}
languageLabel.text = "hello".localized
languageButton.setTitle(language.rawValue, for: .normal)
}
@IBOutlet weak var languageLabel: UILabel!
@IBOutlet weak var languageButton: UIButton!
@IBAction func changeLanguageButtonTapped(_ sender: UIButton) {
updateLanguage()
}
}
Swift : How to change language inside app?
found my answer :
NSUserDefaults.standardUserDefaults().setObject(["language identifier"], forKey: "AppleLanguages")
NSUserDefaults.standardUserDefaults().synchronize()
unfortunately user must restart the app!
if anyone could find a solution to not restart the application please inform me.
Change iOS app language without rebooting the device
You can't change the language of the device in your application but you can change it just for your application by changing the AppleLanguages
property in NSUserDefaults
. Please note however I believe this still requires restarting the app itself but doesn't require a device restart.
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"nl", @"en", nil] forKey:@"AppleLanguages"];
Here's another similar question that also maybe able to help you
Related Topics
What Is the Impact of the "Requires Full Screen" Option in Xcode for an Iphone-Only App
Loading a Welcome Screen (Splash Screen) Before Tabbarcontroller
How to Use the Default iOS7 Uianimation Curve
Push View Programmatically in Callback, Swiftui
How to Wait for Method That Has Completion Block (All on Main Thread)
Get Current View Controller from the App Delegate (Modal Is Possible)
Uilocalnotification Isn't Working at All
Import Framework in Swift Project, Xcode
The Simulator Can't Be Launched Because It Is Already in Use
How to Efficiently Find Cgrects for Visible Words in Uitextview
App Crashes After Executing Background Fetch Completionhandler
How to Set the Tab Order in iOS
"Can't Find Model for Source Store" Occurring During iPhone "Automatic Lightweight Migration"
How to Port "Method_Getimplementation" and "Method_Setimplementation" to Monotouch