2012-03-19 40 views
1

人們如何評論他們的NSLocalizedString?我們應該遵循一個標準指南嗎?例如,如果我有:NSLocalizedString中的評論標準

NSLocalizedString(@"Tap your account to sign in", @""); 

,我的評論是「文本,要求用戶通過點擊該帳戶登錄」,這是一個有點ambigous?如果這個評論幾乎不言自明,我是否應該留下評論?

另一個問題是,如果我有一堆ProgressHUD文本設置爲LoggingIn,那麼這將是一個簡單的方法來同步跨我的應用程序項目,這需要本地化爲NSLocalizedString (@"Logging In", @"some description");是否有工具可以執行這樣的任務?

+1

看看在Mac應用程序林冠 - 從我所聽到的它非常適合幫助本地化應用程序。 – mattjgalloway 2012-03-19 20:09:21

回答

3

第二個參數是一個註釋,如果您使用生成器命令行實用程序,該實用程序可以通過掃描源代碼爲您創建字符串文件,該註釋將自動出現在字符串文件中。

該評論對您的本地化者很有用。例如:

NSLocalizedString(@"Save",@"Title of the Save button in the theme saving dialog"); 

當您運行genstrings,這將產生在Localizable.strings入口文件是這樣的:

/* Title of the Save button in the theme saving dialog */ 
"Save" = "Save"; 

在你的具體的例子,這是相當明顯的是什麼意見手段,但而不是上下文。您應該添加一些上下文,如下所示:

NSLocalizedString(@"Tap your account to sign in", @"Instruct user to tap their account to sign in (Facebook account, main game preferences)"); 

這樣,本地化器就知道您指的是哪個按鈕。

,這變得甚爲標有「分享」或其他一些非特異性的標籤按鈕更重要的是:

NSLocalizedString(@"Share", @"Label for sharing button on main image editing screen"); 

(這就是我的回答修改後的版本,以this similar question)。

+0

+1 ** for ** genstrings **!你爲我節省了很多時間! :) – Chris 2012-07-12 08:00:38

0

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將返回評論,即「設置」

瞧:-)