gpt4 book ai didi

iphone - iOS 本地化, label with number and word together

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:38 26 4
gpt4 key购买 nike

我想本地化我的游戏。我的一些标签就像 [@"Score: %i", score],分数可以是任何数字。那么我还可以使用字符串文件本地化吗?这就是我目前所做的,但需要做很多工作

    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];

这里我把@"Score"和@"%i"分开,score分成2个标签(word和number)分别放置。那么我怎样才能把它们放在一个标签中呢?我可以在 localization.strings 文件中放入“NSString stringWithFormat”吗?

最佳答案

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

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

你的英语字符串文件是:

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

%d%i 没有区别。它们均由标准指定。我个人更喜欢表达它是“十进制”(相对于 %x 十六进制)而不是“整数”(相对于 %f float )。不过没关系。

您应该在字符串文件中包含整个格式语句。您正在本地化整个格式语句 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 进行审核。

关于iphone - iOS 本地化, label with number and word together,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9106339/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com