How do I localize a string with formatting placeholders?
How do I localize a string that has placeholders in it with NSLocalizedString?
[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?
- How do I symbolicate a crash report of a Mac OS X app that a user emailed to me?
- Background location tracking: iOS
- For Soap NSURLConnection, Please ensure that your app supports IPv6 networks, as IPv6 compatibility is required
- How do I write and compile Objective-C code?
- Objective-C: Extract filename from path string
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];
- iOS In App Purchase: test a real purchase without submitting to Apple
- how to pass argument for gesture selector
- Will Facebook iOS SDK 3.0 not work on iOS 3.x?
- Get the data from NSURLSession DownloadTaskWithRequest from completion handler
- Draw a line with a CALayer
- NSCF Array Out of Bounds?
- Wrong polyline drawing on map with Google Maps SDK
- setBackgroundImage forBarMetrics image size?
- Change splash screen image programmatically
- Autolayout height equal to MAX(multiple view heights)
- NSURLSession background upload not working
- Problematic NSManagedObject accumulation over time
- Static Library path nightmare from hell
- Draw a Line Sprite Between Two Points made by Sprites in Cocos2d
- Rejection: “2.23: Apps must follow the iOS Data Storage Guidelines”, We do not have any data saved in documents folder