aignetti
aignetti

Reputation: 491

NSLocalizedString with variables doesnt work for me

so i have the following code:

GameScene:

Rule = Level / 10
let StrRule = String(Rule) //for example Level = 24 -> Rule = 2

let RegelString = String(format: NSLocalizedString("RegelText", comment: ""), StrRule)

//Give the String out in an UILabel as text
RegelLabel.text = RegelString

Thats how my Localizable.strings looks like:

"RegelText" = "You lose %d goals if you miss.";

So i start the game and my Level is 24. So Rule should be 2.

So i build and run but when i start the App he shows me some crazy numbers. The Label is like: You lose 1314063968 goals if you miss.

Why it doesnt work?

Upvotes: 0

Views: 520

Answers (1)

Blip
Blip

Reputation: 1188

%d is for numbers. StrRule is obviously not a number. Are you intending to use %s? RegelText should be like this:

"RegelText" = "You lose %s goals if you miss.";

Or just use Rule and not convert it into a string. The code could be like this:

Rule = Level / 10
let RegelString = String(format: NSLocalizedString("RegelText", comment: ""), Rule)

In this case RegelText would still use %d.

In addition, I don't see that you rounded the result of Level / 10. I know that dividing an integer by an integer results in an integer, not a decimal. But to be on the safe side I think you should put the Level / 10 in a round or floor function.

Upvotes: 1

Related Questions