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"];
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.
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.
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.
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() }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With