iOS 制作的framework里使用国际化方案
来源:互联网 发布:网络运维与管理pdf 编辑:程序博客网 时间:2024/06/03 23:48
f
Dale Zak
Redefining NSLocalizedString for iOS Framework Bundles
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
- iOS 制作的framework里使用国际化方案
- ios 制作使用FrameWork
- iOS在Xcode 7.2 里制作 Framework 的简单过程
- ios-framework的制作
- ios 国际化的使用
- iOS-Universal-Framework 制作 framework的问题
- ios 制作自已的framework
- ios 制作自已的framework
- ios 制作自已的framework
- ios 制作自己的framework
- ios 制作自已的framework
- ios 制作自已的framework
- ios 制作自已的framework
- ios 制作自已的framework
- ios 制作自已的framework
- ios 制作自已的framework
- iOS 制作自已的framework
- iOS中Framework的制作
- IIS 负载均衡
- Java 版在线查看 pdf
- junit忽略测试方法
- hdu 3549 Flow Problem【最大流问题】【FF】
- IOS开发: storyboard之 prepareForSegue:sender: && 设置为跟控制器
- iOS 制作的framework里使用国际化方案
- matlab修改打开时的默认路径的最简单使用的方法
- java POI导出excel(带有图片的数据)以及ftp上传图片到另一台服务器的方法参考代码
- GreenDao自带异步操作类简析
- java I/O流详解
- 《java设计模式》责任链模式
- Android-PullToRefresh onRefreshComplete()失效问题
- HSQLDB内存数据库
- 利用POI做导入EXCEL并插入数据库