本地化故事板即时本地化

我正在开发一个应用程序,它有一个切换按钮,可以在英语和阿拉伯语之间切换,应该是动态的。 我正在使用https://github.com/maximbilan/ios_language_manager中的方法,它在所有情况下都能正常工作,除非故事板是通过接口而不是字符串本地化的:

在此处输入图像描述

现在当我像这样重新加载根视图控制器时:

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

它使用本地化字符串和RTL重新加载根,但英语故事板不是阿拉伯语故事板。

尝试加载这样的阿拉伯语:

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

但不幸的是它加载了故事板但没有图像。 它无法读取任何资源图像。

我最后将阿拉伯语故事板移到外面,并将其命名为Main-AR,然后在UIStoryboard添加一个扩展名,以UIStoryboard故事板的UIStoryboard和初始化程序,如果我使用的是阿拉伯语模式,则将-AR添加到故事板名称的末尾。

 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) } } 

更改用于初始化故事板的包:

  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()) 

虽然这会根据语言改变故事板但不加载图像! 🙁