How to use Localizable.strings stored in a CocoaTo

2019-04-04 08:58发布

I'd like to add multi-language support to a CocoaTouch Framework.

The problem: The Localizable.strings file I created only gets used by NSLocalizedString when it's part of the main app and its target. I'd like to store it inside the Framework to keep things separate.

How can I use the Localizable.strings when placed inside a CocoaTouch Framework?

2条回答
干净又极端
2楼-- · 2019-04-04 09:44

This can be done by specifying the bundle identifier of the framework in the NSLocalizedString constructor.

if let bundle: NSBundle = NSBundle(identifier: "com.mycompany.TheFramework") {
    let string = NSLocalizedString("key", tableName: nil, bundle: bundle, value: "", comment: "")
    print(string)
}
查看更多
Bombasti
3楼-- · 2019-04-04 09:53

Use:

NSLocalizedString("Good", tableName: nil, bundle: NSBundle(forClass: self), value: "", comment: "")

or you can create public func like this:

class func POLocalized(key: String) -> String{
        let s =  NSLocalizedString(key, tableName: nil, bundle: NSBundle(forClass: self.classForCoder()), value: "", comment: "")
        return s
    }

example of use:

let locString = POLocalized("key")
查看更多
登录 后发表回答