Moshe Moshe - 1 year ago 54
iOS Question

How do I localize a string with formatting placeholders?

How do I localize a string that has placeholders in it with NSLocalizedString?

For example:

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

How do I localize this? Do I do break up the strings into multiple localized strings? How then do I deal with varying sentence structure and grammar?

Answer Source

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]

(Caution: I've never used Objective-C and hate the syntax with a vengeance; but I'm pretty adamant about proper localization)