How do I localize a string with formatting placeholders?

4 Solutions Collect From Internet About “How do I localize a string with formatting placeholders?”

Have the localized strings include the placeholders. That’s pretty much the only proper way to do it as otherwise, as you mentioned, you couldn’t take varying word order into account.

Something along these lines:

[NSString stringWithFormat:NSLocalizedString(@"Foo %i", @"Foo %i"), 123]

NSLocalizedString won’t alter your placeholders, so stringWithFormat can use them as normal. In your example, using numbered placeholders is probably a good idea –

    [NSString stringWithFormat:@"You can afford %1$i at %2$@%3$li.",
                              [kCash integerValue]/self.price, kYen,  self.price]

More info here:
Is there a way to specify argument position/index in NSString stringWithFormat?

Another approach to this is in your localized file, you can have:

key = "String with %@ placeholder"; 

and in your implementation:

[NSString stringWithFormat: NSLocalizedString(@"key", ""), @"string replacing placeholder"];

You can do this with any number of arguments, they just need to be consistent across your localization files.

This way seems more efficient:

[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"Key", @"Comment for localised string"), value];