Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you specify the locale/language for an NSFormatter that doesn't have a locale property?

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?

Edit:

Since I wrote this question the names of a lot of the formatters above have changed. Most (all?) have lost their "NS" prefix.

like image 725
Duncan C Avatar asked Apr 10 '16 16:04

Duncan C


1 Answers

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.

like image 197
Rob Napier Avatar answered Oct 06 '22 01:10

Rob Napier