本地化故事板即时本地化

时间:2016-05-03 13:22:33

标签: ios swift localization storyboard

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

enter image description here

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

   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")!))

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

2 个答案:

答案 0 :(得分:1)

我最后将阿拉伯语故事板移到外面,并将其命名为Main-AR,然后在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) } } 我在阿拉伯语模式。

{{1}}

答案 1 :(得分:0)

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

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

虽然这会改变基于语言的故事板但不加载图像! :(