iOS本地化,数字和文字一起标签
我想本地化我的游戏。我的一些标签就像[@“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,你可以这样做。它看起来像这样(未编译;希望没有拼写错误):
您的英文字符串文件将是:
%d
和%i
之间没有区别。它们都是由标准指定的。我个人更喜欢表达它是“十进制”(相对于%x
十六进制),而不是表示它是“整数”(相对于%f
浮点数)。但这并不重要。您应该在字符串文件中包含整个格式语句。您正在本地化整个格式语句
分数:%d
。这样做的理由是允许顺序可能不同的语言。例如,如果某种语言的正确翻译是“%d 分数”
,或者某种语言的意思是有意义的,则您必须说相当于分数:%d 分
code> 或分数:%d 个数字
。如果您不想要这种本地化,并且无论使用哪种语言,您总是会在标签后面放置一个冒号,然后在后面放置一个数字,那么您可以像在原始代码中一样仅本地化标签,或者像这样:
您应该避免按照您的建议使用可变参数调用
NSLocalizedString
。这很令人困惑,并且可能会妨碍使用 genstrings 来创建字符串文件。也就是说,以下代码不是问题:(在 Labels.strings 中):
需要注意的事项:
Labels.strings
的单独字符串文件。这样就不会影响使用genstrings
和主Localizeable.string
。Labels.strings
进行审核。Yes, you can do this. It looks like this (uncompiled; hopefully there is no typo):
Your strings file for this in English would be:
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 ofScore: %d points
orScore: %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:
You should avoid calling
NSLocalizedString
with a variable parameter as you're suggesting. This is confusing, and can get in the way of usinggenstrings
to create your strings file. That said, the following code is not a problem:(In Labels.strings):
Things to note:
Labels.strings
. That way it doesn't impact usinggenstrings
and the mainLocalizeable.string.
Labels.strings
.