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()
}
}
How to programmatically access the apps language in iOS 13?
You may try the below code to get the application preferred language.
let appLang = Locale.preferredLanguages[0]
Change Language in the app programmatically in iOS
Usually when you support official languages that Apple supports in iOS, there is no reason to provide language switching within the app, just properly set up translations in your project and interface language will automatically switch with the system. But since you want it from the app, there are few ways to go about this:
1) You could force a specific language for just your app with the following code:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"zh-Hans", @"en", @"fr", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
I would suggest putting this code inside main.m file in "int main" function just before the "return UIApplicationMain". But this method requires you to kill the app or tell user to restart the app for it to take effect.
You can kill the app without having user to force quit the app using exit(0)
, but make sure user has chance to abort the action with UIAlertView
or similar, or Apple might reject your app.
2) Alternative is implementing your own localization logic, where you just take translations from your own language file. One way is this example which takes translations from official lproj files. This way you can change the language on the fly without a restart, but you have to manually load all label texts from the code. When you change the translation, you have to repopulate the text on screen.
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.
Related Topics
Finish All Asynchronous Requests Before Loading Data
Objective-C Find Caller of Method
Iphone: How to Get Current Milliseconds
How to Hide Keyboard in Swift on Pressing Return Key
Load View from an External Xib File in Storyboard
How to Dismiss the Keyboard When Editing a Uitextfield
How to Assign an Action for Uiimageview Object in Swift
How to Scroll to the Bottom of a Uitableview on the iPhone Before the View Appears
Xcode - Ld: Library Not Found for -Lpods
How to Correctly Handle Weak Self in Swift Blocks with Arguments
How to Calculate Uilabel Width Based on Text Length
Silent Push Notification in iOS 7 Does Not Work
Uicollectionview: Animate Cell Size Change on Selection
iOS Keeping Old Launch Screen and App Icon After Update
Secitemadd and Secitemcopymatching Returns Error Code -34018 (Errsecmissingentitlement)
My Prerelease App Has Been "Processing" for Over a Week in Itunes Connect, What Gives