开发者

Xcode 4: Framework localization not working

I am developing my own framework using Xcode 4, and I'm using it in two sample apps (a console on开发者_如何转开发e, and a Mac OS X Cocoa application).

I'm trying to add localization to the framework, so I've created two versions of a Localizable.strings file (en and fr versions), but every time I'm trying to print a localized string from the sample apps, I only get its technical name. For example, with the following line inside the framework's code:

NSLog(NSLocalizedString(@"LOC_TEST", nil));

I only get "LOC_TEST" displayed in the output...

Localization works fine with the Cocoa app itself however (meaning the Cocoa app's localized strings are shown appropriately).

Following this article, I have tried to add localizations in the framework's plist file:

<key>CFBundleLocalizations</key>
<array>
    <string>en</string>
    <string>fr</string>
</array>

But it didn't change anything...

What am I missing?


The cause for not picking a right .strings file by framework is in NSLocalizedString macro:

#define NSLocalizedString(key, comment) [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]

Framework's location of .strings file is not [NSBundle mainBundle] as specified in macro.

From what I see, you'd need to use NSLocalizedStringFromTableInBundle instead and specify your framework's bundle location.


The accepted answer is correct, NSLocalizedString will not work as it using [NSBundle mainBundle] so I am using this macro in my framework

#define LOCALIZED_STRING(key) [[NSBundle bundleForClass:[self class]] localizedStringForKey:(key) value:@"" table:nil]


The "technical name" as you call it is actually a Key. Your .strings files will look something like:

/* no comment */ "LOC_TEST" = "LOC_TEST";

In en.lproj/Localizable.strings, replace the second LOC_TEST (the one after the =) with your English string. Do the same in fr.lproj with your French text.


example:

put it in your .m file:

#ifndef NSFrameworkLocalizedStrings
#define NSFrameworkLocalizedStrings(key) \
NSLocalizedStringFromTableInBundle(key, @"YOUR_BUNDLE_NAME", [NSBundle bundleWithPath:[[[NSBundle frameworkBundle] resourcePath] stringByAppendingPathComponent:@"YOUR_BUNDLE_NAME.bundle"]], nil)
#endif

and use this method:

+ (NSBundle *)frameworkBundle   {
    static NSBundle* frameworkBundle = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        frameworkBundle = [NSBundle bundleForClass:[self class]];
    });

    //NSLog(@"frameworkBundle %@",frameworkBundle);
    return frameworkBundle;
}

Enjoy √ http://www.LegoTechApps.com (Download Framework that will save your time)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜