您的位置:首页 > 移动开发 > IOS开发

iOS 制作的framework里使用国际化方案

2016-01-28 16:18 567 查看
f


Dale
Zak

Be the change you wish to see in the world – Gandhi


Redefining
NSLocalizedString for iOS Framework Bundles

December
8, 2012 · code, development

In the upcoming version of the Ushahidi iOS
SDK, I ran into a snag getting translations to work inside my custom iOS framework.





The trick is to ensure that you include the Localizable.strings in the Copy
Files section in the Build Phases of your framework bundle.





You can double check whether your Localizable.strings were included correctly
by expanding the bundle in the project using the framework.





The problem however is that the NSLocalizedString macro is defined to
use [NSBundle mainBundle], which means it won’t find your Localizable.strings since
they live inside your separate framework bundle.

#define
NSLocalizedString(key, comment) \

[[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]


The solution is to instead use NSLocalizedStringFromTableInBundle, which
allows you to provide your ownNSBundle for the string lookup.

#define
NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) \

[bundle localizedStringForKey:(key) value:@"" table:(tbl)]


However I didn’t what to replace all my existing NSLocalizedString statements
withNSLocalizedStringFromTableInBundle, so instead I re-defined the
macro in my SDK-Prefix.pch:

#undef
NSLocalizedString

#define NSLocalizedString(key, comment) \

[[NSBundle bundleWithName:@"Ushahidi.bundle"] localizedStringForKey:(key) value:@"" table:nil]


To help with the bundle lookup, I added NSBundle+USH.h category to provide
an easy way to obtain a bundle by name.

+
(NSBundle*) bundleWithName:(NSString*)name {

NSString *mainBundlePath = [[NSBundle mainBundle] resourcePath];

NSString *frameworkBundlePath = [mainBundlePath stringByAppendingPathComponent:name];

if ([[NSFileManager defaultManager] fileExistsAtPath:frameworkBundlePath]){

return [NSBundle bundleWithPath:frameworkBundlePath];

}

return nil;

}


Voila! All my existing NSLocalizedString statements
now return the correct translated phrases from the framework bundle, without any additional changes in the framework code.
http://dalezak.ca/2012/12/nslocalizedstring-framework.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: