There are a lot of specialized subclasses of NSFormatter:
CNContactFormatter
CNPostalAddressFormatter
DRMSFFormatter
MKDistanceFormatter
NSByteCountFormatter
NSDateComponentsFormatter
NSDateFormatter
NSDateIntervalFormatter
NSEnergyFormatter
NSLengthFormatter
NSMassFormatter
NSNumberFormatter
NSPersonNameComponentsFormatter
Most of us have used NSNumberFormatter
and NSDateFormatter
. Those have a locale property that lets you specify what locale to use when converting from strings to/from the "native" format the formatter supports. Some others like NSDateComponentsFormatter
inherit their locale from one of their properties (NSDateComponentsFormatter
uses the locale of the specified calendar, if any.)
Most of the others don't seem to have any way to specify a locale. The only way I can think of to get them to generate output in a specific language/locale is to set the system locale, get the output/convert a string to numeric format, and then set it back.
Is there a way to set the locale for formatters like NSByteCountFormatter
that don't seem to offer a locale setting of any kind?
Since I wrote this question the names of a lot of the formatters above have changed. Most (all?) have lost their "NS" prefix.
It seems very unlikely. If you walk through the implementation (in Hopper), NSByteCountFormatter uses an internal function _NSLocalizedFileSizeDescription
, which relies heavily on -[NSBundle localizedStringForKey:value:table:]
(calling into Foundation's bundle). There's nowhere to pass a locale into that. I would open a radar requesting a numberFormatter property for NSByteCountFormatter to match NSEnergyFormatter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With