I want to display a nicely formatted integer value via the usual pluralization technology .stringsdict
file. (Apple docs here.)
So if I have one thing, I want my output to read
1 thing
But if I have two thousand, three hundred sixty-four things, I want those commas:
2,364 things
but alas the string pluralization technology doesn't give them to me:
2364 things
I can achieve commas with an NSNumberFormatter
. I can achieve pluralization with .stringsdict
localization technology.
Can I easily achieve both?
Since you already have .stringsdict
files setup with the properly plural strings, you don't actually need a number formatter. Simply use the localizedStringWithFormat
method of String
or NSString
.
Assuming you have setup the localized key "%d things"
in your Localizable.strings files and your .stringsdict
files, your code would look something like:
Swift:
let things = 2364
let localizedString = String.localizedStringWithFormat(NSLocalizedString(@"%d things", @"Your comment"), things)
Objective-C:
int things = 2364;
NSString *localizedString = [NSString localizedStringWithFormat:NSLocalizedString(@"%d things", @"Your comment"), things];
Depending on your locale, the result will be something like:
2,364 things
2.364 things
2 364 things