如何为没有语言环境 属性 的 NSFormatter 指定 locale/language?

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

NSFormatter 有很多专门的子类:

CNContactFormatter
CNPostalAddressFormatter
DRMSFFormatter
MKDistanceFormatter
NSByteCountFormatter
NSDateComponentsFormatter
NSDateFormatter
NSDateIntervalFormatter
NSEnergyFormatter
NSLengthFormatter
NSMassFormatter
NSNumberFormatter
NSPersonNameComponentsFormatter

我们大多数人都使用过 NSNumberFormatterNSDateFormatter。那些有一个语言环境 属性 可以让您指定从字符串 to/from 转换为格式化程序支持的“本机”格式时要使用的语言环境。其他一些像 NSDateComponentsFormatter 从它们的属性之一继承它们的语言环境(NSDateComponentsFormatter 使用指定日历的语言环境,如果有的话。)

大多数其他人似乎没有任何方法来指定语言环境。我能想到的让它们以特定 language/locale 生成输出的唯一方法是设置系统区域设置,将 output/convert 字符串转换为数字格式,然后将其设置回去。

有没有办法为像 NSByteCountFormatter 这样似乎不提供任何语言环境设置的格式化程序设置语言环境?

编辑:

自从我写了这个问题之后,上面的许多格式化程序的名称都发生了变化。大多数(全部?)都失去了他们的“NS”前缀。

这似乎不太可能。如果你遍历实现(在 Hopper 中),NSByteCountFormatter 使用一个内部函数 _NSLocalizedFileSizeDescription,它在很大程度上依赖于 -[NSBundle localizedStringForKey:value:table:](调用 Foundation 的包)。没有地方可以将语言环境传递给它。我会打开雷达请求 numberFormatter 属性 NSByteCountFormatter 以匹配 NSEnergyFormatter.