Based on this SO question asked a few hours ago, I have decided to implement a swizzled method that will allow me to take a formatted NSString
as the format arg
How about defining your own interim method instead of using format specifiers and stringWithFormat:
? For example, you could define your own method replaceIndexPoints:
to look for ($1)
instead of %1$@
. You would then format your string and insert translated replacements independently. This method could also take an array of strings, with NSNull
or empty strings at the indexes that don't exist in the “untranslated” string.
Your method could look like this (if it were a category method for NSMutableString
):
- (void) replaceIndexPointsWithStrings:(NSArray *) replacements
{
// 1. look for largest index in "self".
// 2. loop from the beginning to the largest index, replacing each
// index with corresponding string from replacements array.
}
Here's a few issues that I see with your current implementation (at a glance):
__VA_ARGS__
thingy explained in the comments.while (arg = va_arg(args, id))
, you are assuming that the arguments are nil
terminated (such as for arrayWithObjects:
), but with stringWithFormat:
this is not a requirement.$
and @
in your string format in your arg-loop.uaStringWithFormat:
was passed something larger than a pointer (i.e. long long
if pointers are 32-bit). This may only be an issue if your translations also require inserting unlocalised numbers of long long
magnitude.