Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSLocalizedString: Why is genstrings adding 1$ and 2$ to my format specifiers?

I'm localizing my iPad/iPhone app by using genstrings Classes/*.m to generate the Localizable.strings file. Most strings work fine and I get the expected output, but with some strings genstrings is adding 1$ or 1$ in the middle of my format specifiers, like this:

/* No comment provided by engineer. */
"%@%i" = "%1$@%2$i";

/* No comment provided by engineer. */
"%@: %i" = "%1$@: %2$i";

/* No comment provided by engineer. */
"%@" = "%@";

I've not had this issue with other apps, and as you can see it's not consistent. But it doesn't appear to be random either as when I run genstrings on the same class files again the 1$'s etc are always in the same spot.

I can leave them in and the strings work with the correct formatting, but I have been manually removing them because I'm not sure what the $ will do. It's annoying to have to go through and manually update the strings (plus I'll likely miss one eventually).

I'm thinking it could be a text encoding issue? Or can I safely leave them in my strings and just ignore them?

like image 726
Domestic Cat Avatar asked Jan 05 '11 21:01

Domestic Cat


3 Answers

N$ tells the printf-style functions and methods the strict order of your format string's fields. That way, when localizing to a language where the order is different, you don't have to modify the code--just the format string:

printf("%s %s\n", "foo", "bar");
printf("%1$s %2$s\n", "foo", "bar");
printf("%2$s %1$s\n", "foo", "bar");

Output:

foo bar
foo bar
bar foo
like image 163
Jonathan Grynspan Avatar answered Nov 15 '22 08:11

Jonathan Grynspan


Those numbers refer to the argument position of the replacement value. For instance, %2$i means "replace this with the second argument, which should be an integer." This is useful when localizing to languages that use different word arrangements, because things can be replaced in an order different from that of English.

In other words, they're safe to leave in or remove, and they'll come in handy for natural-sounding translations to other languages.

like image 23
Justin Spahr-Summers Avatar answered Nov 15 '22 07:11

Justin Spahr-Summers


You can turn off the generation of positional parameters if you don't like it by passing

-noPositionalParameters

to genstrings.

like image 20
Ron Davis Avatar answered Nov 15 '22 08:11

Ron Davis