Force Localize Internal structures like UIKit without restarting the application

I need to add the option to switch the language inside the application. I just went in cycles in localization of internal frames. Can someone help me with localizing the internal structure like UIKit etc. From the application itself without rebooting. My code works fine, but a reboot is required for the internal environment. Only internal structures are not localized.

My current code is:

Create a file called 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 that 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() } 
+5
source share
4 answers

Check out this demo, it will work for you.

Link

+3
source

your code seems good, but I tried ...

When you change the language from the application, then ...

 Bundle.setLanguage("fr") UserDefaults.standard.set(["fr"], forKey: kAppleLanguages) UserDefaults.standard.synchronize() NotificationCenter.default.post(name: NSNotification.Name.init("AppLanguageChangeNotification"), object: nil) 

therefore, when the language changes, this notification will be launched, and the view controller that added the observer to it will be notified and it is necessary to change in accordance with this language.

Add Observer

 NotificationCenter.default.addObserver(viewController, selector: #selector(self.setLocalization), name: NSNotification.Name.init("AppLanguageChangeNotification"), object: nil) 

Method

 @objc fileprivate func setLocalization() { self.lblProfile.text = NSLocalizedString("lblProfile", comment: "") self.lbDetails.text = NSLocalizedString("lblDetails", comment: "") . . } 

Yes, this is a bit-log process, since you need all IBOutles to set localized text.

0
source

It depends on what specific structure you want to localize. This is necessary in order to know where their localizations come from from this structure. For most cases this is not possible, because you do not have to hardcode the path to the framework localization file or know something about a structure that violates one of the framework’s ideas: to know less about the implementation, all you need to know is the interface.

To achieve what you want, you must have a single source for localization.

With the help of containers or some other non-system frameworks, you can fork and modify them so that you can pass your lines.

With system libraries, this may not be possible because there is no way to change it in any way. But all the cases that I know can be implemented using these system libraries, I say, for example, about the localized buttons of the navigation bar. You can create your own button and set its name to whatever you want.

Hope this helps. If not, you can show an example of what exactly you cannot localize, and I will try to help you.

0
source

I ran into the same problem, and only the solution for me completely rebooted all the UIViewController

0
source

Source: https://habr.com/ru/post/1275013/


All Articles