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

4

19

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?

Willhite answered 25/2, 2015 at 16:46 Comment(0)
E
15

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")
Envious answered 22/7, 2015 at 14:1 Comment(5)
Do not use NSBundle(forClass: self) for public classes, because some Main bundle classes can inherit framework class and NSBundle(forClass: self) will point to main bundle, not the framework.Ascomycete
In this case u can use NSBundle(identifier: "FRAMEWORK_ID")Envious
What does the value parameter represent? The current Apple docs do not explain...Ozonolysis
It's a default value returned if the string is not found in the given table.Interview
For those who might wonder: No matter how much localizations your framework may contain, they will not be returned by NSLocalizedString (with the solution above) if your project itself is not targeting the desired language also.Sorensen
T
7

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)
}
Tumbledown answered 24/2, 2016 at 23:43 Comment(0)
L
4

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.

Locker answered 27/4, 2019 at 22:6 Comment(1)
Code only answers are discouraged. Please add some explanation as to how this solves the problem, or how this differs from the existing answers. From ReviewGolightly
A
0

Get Localizable.strings from framework

A main point is to use Framework's bundle - bundle where strings(Localizable.string) are located

[Access to Framework bundle]

//e.g.
let someString = NSLocalizedString("some_string_id", tableName: nil, bundle: Bundle(identifier: "com.something")!, value: "", comment: "")

extension variant

extension String {
    public var localized: String {
        return NSLocalizedString(self, tableName: nil, bundle: Bundle(identifier: "com.something")!, value: "", comment: "")
    }
}

//using
let someString = "some_string_id".localized
Apocynaceous answered 24/6, 2021 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.