Localization on the fly with localized excavations

I am working on an application that has a button to switch between English and Arabic and should be on the fly. I use the method at https://github.com/maximbilan/ios_language_manager , and it works fine in all cases, except when the storyboard is localized on the interface without strings:

enter image description here

Now, when I reboot the root view controller as follows:

func reloadRootVC(){ let delegate : AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) delegate.window?.rootViewController = (storyboard.instantiateInitialViewController()) } 

it reloads the root with localized strings and RTL, but with English storyboard, not Arabic.

I tried an Arab-type power load as follows:

 let storyboard = UIStoryboard(name: "Main", bundle: NSBundle(path: NSBundle.mainBundle().pathForResource(LanguageManager.currentLanguageCode(), ofType: "lproj")!)) 

but unfortunately, it loads the storyboard, but without images. He cannot read the image of the resource.

+4
source share
2 answers

I ended up moving the Arabic storyboard and named it Main-AR, and then added an extension to the UIStoryboard for swizzle and the storyboard initializer to add -AR to the end of the storyboard name if I'm in Arabic mode.

 extension UIStoryboard { public override class func initialize() { struct Static { static var token: dispatch_once_t = 0 } // make sure this isn't a subclass if self !== UIStoryboard.self { return } dispatch_once(&Static.token) { let originalSelector = #selector(UIStoryboard.init(name:bundle:)) let swizzledSelector = #selector(UIStoryboard.initWithLoc(_:bundle:)) let originalMethod = class_getClassMethod(self, originalSelector) let swizzledMethod = class_getClassMethod(self, swizzledSelector) class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)) method_exchangeImplementations(originalMethod, swizzledMethod) } } // MARK: - Method Swizzling class func initWithLoc(name: String, bundle storyboardBundleOrNil: NSBundle?) -> UIStoryboard{ var newName = name if LanguageManager.isCurrentLanguageRTL(){ newName += "-AR" if #available(iOS 9.0, *) { UIView.appearance().semanticContentAttribute = .ForceRightToLeft } else { // Fallback on earlier versions } } else{ if #available(iOS 9.0, *) { UIView.appearance().semanticContentAttribute = .ForceLeftToRight } else { // Fallback on earlier versions } } return initWithLoc(newName, bundle: storyboardBundleOrNil) } } 
0
source

change the package that is used to initialize the storyboard:

  let path = Bundle.main.path(forResource: "ar", ofType: "lproj") let bundle = Bundle(path: path!) let delegate : AppDelegate = UIApplication.shared.delegate as! AppDelegate let storyboard = UIStoryboard(name: "Main", bundle: bundle) delegate.window?.rootViewController = (storyboard.instantiateInitialViewController()) 

although this modifies the storyboard based on the language, but does not load the images !: (

0
source

All Articles