iOS本地化,数字和文字一起标签

发布于 2025-01-02 05:56:37 字数 832 浏览 1 评论 0原文

我想本地化我的游戏。我的一些标签就像[@“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”分开,将分数分为2个标签(单词和数字)并将它们分开放置。 那么我怎样才能将它们放入一个标签中呢?我可以将“NSString stringWithFormat”放入localization.strings 文件中吗?

i want to localize my game. some of my label is like [@"Score: %i", score], and the score can be any number. so can i still use strings file localization?
this is what i do currently, but a lot of work

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

here i separate @"Score" and @"%i", score into 2 labels (word and number) and place them separately.
so how can i put them into one label? can i put like "NSString stringWithFormat" in localization.strings file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡绿茶香 2025-01-09 05:56:37

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

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

您的英文字符串文件将是:

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

%d%i 之间没有区别。它们都是由标准指定的。我个人更喜欢表达它是“十进制”(相对于 %x 十六进制),而不是表示它是“整数”(相对于 %f 浮点数)。但这并不重要。

您应该在字符串文件中包含整个格式语句。您正在本地化整个格式语句分数:%d。这样做的理由是允许顺序可能不同的语言。例如,如果某种语言的正确翻译是“%d 分数”,或者某种语言的意思是有意义的,则您必须说相当于分数:%d 分 code> 或 分数:%d 个数字

如果您不想要这种本地化,并且无论使用哪种语言,您总是会在标签后面放置一个冒号,然后在后面放置一个数字,那么您可以像在原始代码中一样仅本地化标签,或者像这样:

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 进行审核。

Yes, you can do this. It looks like this (uncompiled; hopefully there is no typo):

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

Your strings file for this in English would be:

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

There is no difference between %d and %i. They are both specified by the standard. I personally prefer to express that it is "decimal" (vs. %x hexadecimal) rather than that it is an "integer" (vs. %f float). But it does not matter.

You should include the entire format statement in the strings file. You are localizing the entire format statement Score: %d. The rationale for this is to permit languages where the order might be different. For example, if there were a language where the correct translation were "%d score" or a language where to make sense you would have to say the equivalent of Score: %d points or Score: %d number.

If you don't want this kind of localization, and you will always put a colon after the label and a number after that, no matter the language, then you can localize just the label as you were in your original code, or like this:

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

You should avoid calling NSLocalizedString with a variable parameter as you're suggesting. This is confusing, and can get in the way of using genstrings to create your strings file. That said, the following code is not a problem:

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

...

(In Labels.strings):

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

Things to note:

  • I created a separate strings file for this called Labels.strings. That way it doesn't impact using genstrings and the main Localizeable.string.
  • I used string constants for the keys. That puts all the strings used in one place at the top of the file, making it easy to audit against Labels.strings.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文