I have tried to find an answer to this question, but could not. How do we specify which of the placeholders for a string need to be reordered when the string is localized, eg
"generalSiteIdentity" = "This website is owned by %@\nThis has been verified by %@"
might need to be translated as
"generalSiteIdentity" = "%@ has verified that %@ owns this website.";
Is there a way to specify that the "owner" placeholder is specified first in the code:
L10n.generalSiteIdentity("Sarah", "Tina")
This is totally possible but not just only related to SwiftGen itself. You should look about the printf-format spec about this.
Basically, if you want to specify an explicit positional order to your placeholders you can do it using n$ between the % and the character giving the placeholder type, eg "%2$@ has verified this website is owned by %1$@."
If you don't specify n$ positional indexes in your placeholders the arguments are gonna be taken in order but if you use positional indexes they'll sourdough the index of the argument to use.
This whole thing is not specifically related to SwiftGen. That's how printf-like functions work, including String(format: …).
For more info on this, read https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
Note that you can also use the “n$” positional specifiers such as %1$@ %2$s
And the IEEE prinf spec linked in that article: http://pubs.opengroup.org/onlinepubs/009695399/functions/printf.html
Conversions can be applied to the nth argument after the format in the argument list, rather than to the next unused argument. In this case, the conversion specifier character % (see below) is replaced by the sequence "%n$", where n is a decimal integer in the range [1,{NL_ARGMAX}], giving the position of the argument in the argument list.
_(And thank God the spec supports that, otherwise localization would be a nightmare, with or without SwiftGen, as otherwise you'd need to pass your arguments in a different order depending on the user's locale! So of course Foundation supports this out of the box!)_
Also note that if needed, you can use the same positional index multiple times in a format string, like let s = String(format: "I saw %1$@ ate %2$d apples. Yes, those %2$d apples were really eaten by %1$@, I assure you", "John", 3)
Of course this format can be used by String(format:), printf, NSLog and all those family of functions, as they all follow the IEEE spec.
And SwiftGen supports it as well completely (we even have tests using it, like this example generating this function which, as you can see takes the right parameter types in the order matching the positional indexes used in the Localizable.strings files.
_Closing as this seems solved to me (question/solution not specific to SwiftGen but to String(format😃 & NSLocalizedString & localisation in general), feel free to reopen if needed for positional parameters specific to SwiftGen though_
Thank you very much! I hadn't used positional specifiers before and as I said, couldn't find any reference to them in the SwiftGen documentation.
Sure, well that's because as stated this has nothing to do with SwiftGen. You'd have the very same question even if you used String(format: NSLocalizedString("generalSiteIdentity", ""), owner, verifier) manually without using SwiftGen at all :wink:
Most helpful comment
For more info on this, read https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
And the IEEE prinf spec linked in that article: http://pubs.opengroup.org/onlinepubs/009695399/functions/printf.html
_(And thank God the spec supports that, otherwise localization would be a nightmare, with or without SwiftGen, as otherwise you'd need to pass your arguments in a different order depending on the user's locale! So of course Foundation supports this out of the box!)_
Also note that if needed, you can use the same positional index multiple times in a format string, like
let s = String(format: "I saw %1$@ ate %2$d apples. Yes, those %2$d apples were really eaten by %1$@, I assure you", "John", 3)Of course this format can be used by
String(format:),printf,NSLogand all those family of functions, as they all follow the IEEE spec.And SwiftGen supports it as well completely (we even have tests using it, like this example generating this function which, as you can see takes the right parameter types in the order matching the positional indexes used in the
Localizable.stringsfiles.