Rob Keniger是對的。我也想補充一點: 第二個參數可以用作.. 默認值 !!
(NSLocalizedStringWithDefaultValue都不能正確使用genstring爲什麼我提出這個解決方案的工作,這是)
下面是使用NSLocalizedString使用默認值評論我的自定義實現:
1。在您的預編譯頭(.PCH文件),重新定義了 'NSLocalizedString' 宏:
// cutom NSLocalizedString that use macro comment as default value
#import "LocalizationHandlerUtil.h"
#undef NSLocalizedString
#define NSLocalizedString(key,_comment) [[LocalizationHandlerUtil singleton] localizedString:key comment:_comment]
2.創建一個類來實現本地化處理
#import "LocalizationHandlerUtil.h"
@implementation LocalizationHandlerUtil
static LocalizationHandlerUtil * singleton = nil;
+ (LocalizationHandlerUtil *)singleton
{
return singleton;
}
__attribute__((constructor))
static void staticInit_singleton()
{
singleton = [[LocalizationHandlerUtil alloc] init];
}
- (NSString *)localizedString:(NSString *)key comment:(NSString *)comment
{
// default localized string loading
NSString * localizedString = [[NSBundle mainBundle] localizedStringForKey:key value:key table:nil];
// if (value == key) and comment is not nil -> returns comment
if([localizedString isEqualToString:key] && comment !=nil)
return comment;
return localizedString;
}
@end
3.使用它!
請確保您在應用程序構建階段中添加了運行腳本,以便您在每個構建中更新Localizable.strings文件,即
我編階段腳本是一個shell腳本:
Shell: /bin/sh
Shell script content: find . -name \*.m | xargs genstrings -o MyClassesFolder
所以,當你在代碼中添加這個新行:
self.title = NSLocalizedString(@"view_settings_title", @"Settings");
,新的本地化字符串會在你的Localized.strings文件被添加
然後進行構建,您./Localizable.scripts文件將包含這一新行:
/* Settings */
"view_settings_title" = "view_settings_title";
由於可在「view_settings_title」鍵==值,自定義LocalizedStringHandler將返回評論,即「設置」
瞧:-)
看看在Mac應用程序林冠 - 從我所聽到的它非常適合幫助本地化應用程序。 – mattjgalloway 2012-03-19 20:09:21