How to use Localizable.strings stored in a CocoaTouch Framework?

这一生的挚爱 提交于 2019-12-04 01:23:44

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

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

You can add this struct to your codebase:

internal struct InternalConstants {
    private class EmptyClass {}
    static let bundle = Bundle(for: InternalConstants.EmptyClass.self)
}  

And then use optional bundle param to specify the location of your string file:

let yourString = NSLocalizedString("Hello", bundle: InternalConstants.bundle, comment: "")

Bundle.main is the default value if you don't use bundle param in your NSLocalizedString constructor.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!