【问题标题】:Standards for comments in NSLocalizedStringNSLocalizedString 中的注释标准
【发布时间】:2012-03-19 19:59:14
【问题描述】:

人们如何为NSLocalizedStrings 编写 cmets?是否有我们应该遵循的标准指南?例如,如果我有:

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

我的评论是“要求用户通过点击帐户登录的文本”,这有点模棱两可吗?如果它几乎是不言自明的,我应该留下评论吗?

另一个问题是,如果我有一堆将文本设置为 LoggingIn 的 ProgressHUD,那么在我的应用项目中同步需要本地化为 NSLocalizedString (@"Logging In", @"some description"); 的简单方法是什么?执行此类任务?

【问题讨论】:

  • 看看 Mac 应用程序 Linguan - 据我所知,它非常适合帮助本地化应用程序。

标签: objective-c cocoa coding-style localization


【解决方案1】:

第二个参数是一个注释,如果您使用 genstrings 命令行实用程序,它将自动出现在字符串文件中,该实用程序可以通过扫描您的源代码为您创建字符串文件。

该评论对您的本地化人员很有用。例如:

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 的回答的修改版)。

【讨论】:

    【解决方案2】:

    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 文件将在每次构建时更新,即,新的本地化字符串将添加到您的 Localized.strings 文件中:

    我的构建阶段脚本是一个shell脚本:

    Shell: /bin/sh
    Shell script content: find . -name \*.m | xargs genstrings -o MyClassesFolder
    

    因此,当您在代码中添加这一新行时:

    self.title = NSLocalizedString(@"view_settings_title", @"Settings");
    

    然后执行构建,您的 ./Localizable.scripts 文件将包含以下新行:

    /* Settings */
    "view_settings_title" = "view_settings_title";
    

    由于'view_settings_title'的key == value,自定义LocalizedStringHandler将返回注释,即'Settings'

    瞧 :-)

    【讨论】:

      猜你喜欢
      • 2017-10-15
      • 2011-01-03
      • 2015-02-06
      • 2014-11-20
      • 2016-01-19
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      • 2015-10-01
      相关资源
      最近更新 更多