2016-05-03 101 views
4

我正在研究一個具有切換按鈕的應用程序,以便在英語和阿拉伯語之間進行切換,並且應該在飛行中。我用了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")!)) 

但不幸的是它加載的故事板,但沒有圖像。它無法讀取任何資源圖像。

+0

不幸的是,你將會遇到很多麻煩,使它能夠用於接口,以及讓RTL UI的行爲正確,更不用說格式化器和本地化格式字符串沒有做正確的事情。 The一般的期望是,應用程序遵循系統語言,並且不實施應用程序內語言切換器......從長遠來看,它將爲您節省很多麻煩。:( – wakachamo

回答

0

我結束了被外界並將其命名爲主要-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) 
} 
} 
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()) 

雖然這種變化基於語言的故事板,但不加載圖像! :(