by ampatspell
in Code
NSLocalizedString and friends uses [NSBundle mainBundle] to load localized strings from localization tables (by default Localized.strings file). For Framework code, in runtime -mainBundle returns Cocoa application bundle what uses the framework, not the framework bundle itself. But I needed to store localized strings inside a Framework bundle and use them from Framework.
It turns out the solution is really simple — refer bundle by identifier:
[NSBundle bundleWithIdentifier:kZeebaBundleIdentifier]
…or by Objective-C Class what’s stored inside a bundle:
[NSBundle bundleForClass:[Zeeba class]]
So, if framework’s bundle identifier is com.amateurinmotion.Zeeba, the NSLocalizedString helper macro would be:
#define kZeebaBundleIdentifier @"com.amateurinmotion.Zeeba" #define ZeebaLocalizedString(key, comment) \ NSLocalizedStringFromTableInBundle((key) \ nil, \ [NSBundle bundleWithIdentifier:kZeebaBundleIdentifier],\ (comment))
Usage:
ZeebaLocalizedString(@"ZeebaGreeting", @"Huluu zeeba")
To generate Localizable.strings from implementation files what uses ZeebaLocalizedString macro:
genstrings -s ZeebaLocalizedString *.m