2

So, I'm translating my app to another language. I have a few texts with variables interpolated, example:

label.text = String(format: NSLocalizedString("Your name is %d", comment: "label that show user's name"), name)

Now, I go to Localizable.strings file and translate it (I won't actually translate it):

"Your name is %d" = "Your name is %d"

But in my ViewController, the variable %d isn't showing up in letters, but in random numbers, like 782393, etc. Why is this happening? One curious thing is that in one text I did the "same thing":

anotherLabel.text = String(format: NSLocalizedString("Your name is %d and you have %d assignments", comment: "label that show user's name"), name, someArray.count)

When translating:

"Your name is %d$1 and you have %d$2 assignments" = "Your name is %d$1 and you have %d$2 assignments"

The curious thing is that the second %d shows as normal, the number of objects in the array

How it comes this is happening?

Thanks!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Henrique Dourado
  • 320
  • 1
  • 7
  • 16

1 Answers1

2

Things like %d are called format specifiers. They differ depending on what you are trying to print. To print an int you use %d. To print a C style string you use %s and to print an Objective-C string or object you use %@.

In your case, you are seeing some random number because you are trying to print a string formatted as an int. Since your name variable is probably either an Objective-C or Swift string you should be using %@ as your format specifier.

You can find a full listing of available specifiers here: String Format Specifiers

idz
  • 12,825
  • 1
  • 29
  • 40