Hos*_*eeb 5 localization storyboard ios swift
我正在开发一个具有切换按钮的应用程序,该按钮可以在英语和阿拉伯语之间进行切换,并且应该是实时运行的。我正在使用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())
}
Run Code Online (Sandbox Code Playgroud)
它使用本地化字符串和RTL重新加载根目录,但使用英语故事板而不是阿拉伯语故事板。
尝试用力加载阿拉伯语,如下所示:
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle(path: NSBundle.mainBundle().pathForResource(LanguageManager.currentLanguageCode(), ofType: "lproj")!))
Run Code Online (Sandbox Code Playgroud)
但不幸的是,它加载了情节提要,但没有图像。它无法读取任何资源图像。
我最终将阿拉伯语故事板移到外面并将其命名为 Main-AR,然后在UIStoryboard故事板的 swizzle 和初始化程序中添加扩展,以便-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)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3684 次 |
| 最近记录: |