iOS 制作的framework里使用国际化方案
2016-01-28 16:18
567 查看
f
Be the change you wish to see in the world – Gandhi
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.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/26/506f4abc1a15d89690ff4686f0d4cff0.png)
The trick is to ensure that you include the Localizable.strings in the Copy
Files section in the Build Phases of your framework bundle.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/26/00ab361f6e5f62f430c2b32d7c704b1d.png)
You can double check whether your Localizable.strings were included correctly
by expanding the bundle in the project using the framework.
![](http://dalezak.ca/wp-content/uploads/2012/12/ios_app_resources.png)
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.
The solution is to instead use NSLocalizedStringFromTableInBundle, which
allows you to provide your ownNSBundle for the string lookup.
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:
To help with the bundle lookup, I added NSBundle+USH.h category to provide
an easy way to obtain a bundle by name.
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
Dale
Zak
Be the change you wish to see in the world – Gandhi
Redefining
NSLocalizedString for iOS Framework Bundles
December8, 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.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/26/506f4abc1a15d89690ff4686f0d4cff0.png)
The trick is to ensure that you include the Localizable.strings in the Copy
Files section in the Build Phases of your framework bundle.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/26/00ab361f6e5f62f430c2b32d7c704b1d.png)
You can double check whether your Localizable.strings were included correctly
by expanding the bundle in the project using the framework.
![](http://dalezak.ca/wp-content/uploads/2012/12/ios_app_resources.png)
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
相关文章推荐
- Win7+虚拟机OS X 10.11.3+Xcode7.0.1+未越狱iPhone4+iOS7.1.2真机调试及生成ipa包(2016-1-22 19:09)
- IOS随机产生字符串,数字
- iOS 字体
- iOS NSURLSession
- ios 查看模拟器路径以及应用的文件夹
- iOS-NSComparisonResult和NSComparator介绍,比较,排序
- IOS语音集成
- iOS---近来开发中的一些收获
- iOS主题皮肤切换示例Demo下载 一步一步实现主题切换
- 移动web ios
- iOS毛玻璃效果
- iOS上如何让按钮文本左对齐问题
- IOS仿今日头条滑动导航栏
- 关于iOS中的NSURLSession
- iOS开发手写分页
- iOS仿今日头条滑动导航
- iOS CloudKit与Core Data、商业后端服务、自己的服务器相比优势
- iOS---免费获取邓白氏编码
- 坑爹的QQ分享
- iOS Core Data的使用