Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: How to change app language programmatically WITHOUT restarting the app?

When I change the app used language independently on the device language it doesn't take effect until I close the app and restart it. How to not require app to be restarted for loading all nib files and .strings files again depending on the selected language?

I use this to change language at runtime:

NSArray* languages = [NSArray arrayWithObjects:@"ar", @"en", nil];  [[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"]; 
like image 935
Ahmed Said Avatar asked Feb 23 '12 16:02

Ahmed Said


People also ask

How do you change the Flutter app language without restarting the app?

On the MainModel , all you need to do is change the preferredLanguageCode variable to whatever you want ('en', 'ar', 'es', etc). Don't forget to call NotifyListeners() once you change the language.

How do I change the language on my iPhone apps?

Change The App Language in iPhone or iPad AppsTap on the Settings app on the Home screen. Scroll down, select the app you wish to change its language. Select Language under Preferred Language. Choose the language you want to use.

How do I programmatically restart an iOS app?

You cannot restart an iOS Application in any case, even if you're able to do using some private api your application will be rejected by Apple and will not be considered for App store release.


1 Answers

This works for me : Swift 4 :

Create a file named BundleExtension.swift and add the following code to it -

var bundleKey: UInt8 = 0  class AnyLanguageBundle: Bundle {  override func localizedString(forKey key: String,                               value: String?,                               table tableName: String?) -> String {      guard let path = objc_getAssociatedObject(self, &bundleKey) as? String,         let bundle = Bundle(path: path) else {              return super.localizedString(forKey: key, value: value, table: tableName)     }      return bundle.localizedString(forKey: key, value: value, table: tableName)   } }  extension Bundle {  class func setLanguage(_ language: String) {      defer {          object_setClass(Bundle.main, AnyLanguageBundle.self)     }      objc_setAssociatedObject(Bundle.main, &bundleKey,    Bundle.main.path(forResource: language, ofType: "lproj"), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)   } } 

Now whenever you need to change the language call this method :

func languageButtonAction() {     // This is done so that network calls now have the Accept-Language as "hi" (Using Alamofire) Check if you can remove these     UserDefaults.standard.set(["hi"], forKey: "AppleLanguages")     UserDefaults.standard.synchronize()      // Update the language by swaping bundle     Bundle.setLanguage("hi")      // Done to reintantiate the storyboards instantly     let storyboard = UIStoryboard.init(name: "Main", bundle: nil)     UIApplication.shared.keyWindow?.rootViewController = storyboard.instantiateInitialViewController() } 
like image 62
Ankit Kumar Gupta Avatar answered Sep 18 '22 19:09

Ankit Kumar Gupta