本地化故事板即时本地化 [英] Localization on the fly with localized Storyboards

查看:129
本文介绍了本地化故事板即时本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款应用程序,它有一个切换按钮,可以在英语和阿拉伯语之间切换,应该可以随时使用。我正在使用



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

  func reloadRootVC(){

让委托:AppDelegate = UIApplication.sharedApplication()。委托为! AppDelegate

let storyboard = UIStoryboard(名称:Main,bundle:NSBundle.mainBundle())

delegate.window?.rootViewController =(storyboard.instantiateInitialViewController())
}

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



试图用这样的方式加载阿拉伯语:

 让storyboard = UIStoryboard(名称:Main,包:NSBundle(路径:NSBundle.mainBundle()。pathForResource(LanguageManager.currentLanguageCode(),ofType:lproj)!))

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

解决方案

我最后将阿拉伯语故事板移到外面并将其命名为Main-AR,然后在<$ c $中添加扩展名c> UIStoryboard to swizzle和storyboard的初始化程序,如果我在阿拉伯语模式下,将 -AR 添加到故事板名称的末尾。

  extension UIStoryboard {
public override class func initialize(){
struct Static {
static var token:dispatch_once_t = 0
}

//确保这不是子类
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: - 方法Swizzling

class func initW ithLoc(name:String,bundle storyboardBundleOrNil:NSBundle?) - > UIStoryboard {
var newName = name
if LanguageManager.isCurrentLanguageRTL(){
newName + = - AR
if #available(iOS 9.0,*){
UIView.appearance()。semanticContentAttribute = .ForceRightToLeft
} else {
//早期版本的回落

}
}
else {
如果#available(iOS 9.0,*){
UIView.appearance()。semanticContentAttribute = .ForceLeftToRight
} else {
//早期版本的回退
}
}
返回initWithLoc(newName,bundle:storyboardBundleOrNil)
}
}


I'm working on an app that has a toggle button to switch between English and Arabic language and should be on the fly. I'm using the method in https://github.com/maximbilan/ios_language_manager and it works fine in all cases except if the storyboard is localized by interface not strings:

Now when I reload the root view controller like this:

   func reloadRootVC(){

    let delegate : AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())

    delegate.window?.rootViewController = (storyboard.instantiateInitialViewController())
}

it reload the root with localized strings and in RTL but with the english storyboard not the arabic one.

Tried force loading the arabic one like this:

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

but unfortunately it loads the storyboard but with no images. It can't read any resource image.

解决方案

I ended up by moving the arabic storyboard outside and name it Main-AR, then adding an extension in UIStoryboard to swizzle and initializer of storyboard to add -AR to the end of the storyboard name if i'm on 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)
}
}

这篇关于本地化故事板即时本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆