2012-02-02 42 views
2

我想本地化我的游戏。我的一些标签就像[@“分数:%i”,分数],分数可以是任意数字。所以我仍然可以使用字符串文件本地化? 这就是我目前做的,但很多工作iOS本地化,带数字和单词的标签

CCLabelTTF* bestLabelWord = [Helper createLocalizedLabelWithStringUpperCase:@"BEST" color:ccBLACK]; 
    CCLabelTTF* bestLabelNumber = [Helper createUnlocalizedLabelWithString:[NSString stringWithFormat:@"%i", bestScore] color: ccBLACK]; 
    bestLabelWord.anchorPoint = ccp(0, 0.5f); 
    bestLabelNumber.anchorPoint = ccp(0, 0.5f); 
    bestLabelWord.position = ccp(menuPosX, menuPosY2); 
    bestLabelNumber.position = ccp(menuPosX + bestLabelWord.contentSize.width + 5, menuPosY2); 
    [self addChild:bestLabelWord z:kZLabel]; 
    [self addChild:bestLabelNumber z:kZLabel]; 

这里我分开@“得分”,@“%i”的,将比分追成2个标签(字和数字),并分开放置。 那么我怎么能把它们放到一个标签?我可以把“NSString stringWithFormat”放在localization.strings文件中吗?

回答

2

是的,你可以这样做。它看起来像这样(未编译;希望没有错字):

NSString *scoreFormat = NSLocalizedString(@"Score: %d", @"Score format"); 
NSString *scoreString = [NSString stringWithFormat:scoreFormat, bestScore]; 

您的字符串文件对于这个用英语将是:

/* Score format */ 
"Score: %d" = "Score: %d"; 

%d没有区别和%i。它们都是由标准规定的。我个人更喜欢表示它是“十进制”(与十六进制的%x相比),而不是它是“整数”(与浮点数%f相比)。但是这没关系。

您应该将整个格式语句包含在字符串文件中。您正在本地化整个格式声明Score: %d。这样做的基本原理是允许订单可能不同的语言。例如,如果有一种语言的正确译文是"%d score"或语言有意义,那么您将不得不说相当于Score: %d pointsScore: %d number

如果您不想要这种类型的本地化,并且您将始终在后面的标签和数字之后放一个冒号,无论使用何种语言,那么您都可以像原始代码一样将本地化标签或者是这样的:

NSString *localizedLabel = NSLocalizedString(@"Score", @"Score"); 
NSString *scoreString = [NSString stringWithFormat:@"%@: %d", localizedLabel, bestScore]; 

你应该避免调用NSLocalizedString具有可变参数作为你所建议的。这是令人困惑的,并可能妨碍使用genstrings来创建您的字符串文件。这就是说,下面的代码是没有问题的:

NSString * const kScoreLabelKey = @"Score"; 
NSString * const kPriceLabelKey = @"Price"; 

NSString *GetLocalizedStringForKeyValue(NSString *key, NSInteger value) 
{ 
    NSString *label = [[NSBundle mainBundle] localizedStringForKey:key value:nil table:@"Labels"]; 
    NSString *valueLabel = [NSString stringWithFormat:@": %d", value]; 
    return [label stringByAppendingString:valueLabel]; 
} 

... 

NSString *label = GetLocalizedStringForKeyValue(kScoreLabelKey, bestScore); 

... 

(在Labels.strings):

"Score" = "Score"; 
"Price" = "Price"; 

注意事项:

  • 我创建了一个独立的字符串文件此称为Labels.strings。这样,它不会影响使用genstrings和主Localizeable.string.
  • 我用键的字符串常量。这将所有在一个地方使用的字符串放在文件的顶部,这样可以很容易地对Labels.strings进行审核。
+0

什么是@“Score格式”? – OMGPOP 2012-02-02 02:35:11

+0

我应该把什么字符串文件? – OMGPOP 2012-02-02 02:36:01

+0

http://stackoverflow.com/questions/1442822/what-is-second-param-of-nslocalizedstring – 2012-02-02 02:36:29