@@ -33,23 +33,24 @@ internal func resetAllNumberFormatterCaches() {
33
33
@available ( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
34
34
internal class ICUNumberFormatterBase {
35
35
internal let uformatter : OpaquePointer
36
+ /// Stored for testing purposes only
36
37
internal let skeleton : String
37
38
38
- init ? ( skeleton: String , locale : Locale ) {
39
+ init ? ( skeleton: String , localeIdentifier : String , preferences : LocalePreferences ? ) {
39
40
self . skeleton = skeleton
40
41
let ustr = Array ( skeleton. utf16)
41
42
var status = U_ZERO_ERROR
42
- let formatter = unumf_openForSkeletonAndLocale ( ustr, Int32 ( ustr. count) , locale . identifierCapturingPreferences , & status)
43
-
43
+ let formatter = unumf_openForSkeletonAndLocale ( ustr, Int32 ( ustr. count) , localeIdentifier , & status)
44
+
44
45
guard let formatter else {
45
46
return nil
46
47
}
47
-
48
+
48
49
guard status. isSuccess else {
49
50
unumf_close ( formatter)
50
51
return nil
51
52
}
52
-
53
+
53
54
uformatter = formatter
54
55
}
55
56
@@ -246,27 +247,28 @@ internal class ICUNumberFormatterBase {
246
247
final class ICUNumberFormatter : ICUNumberFormatterBase {
247
248
fileprivate struct Signature : Hashable {
248
249
let collection : NumberFormatStyleConfiguration . Collection
249
- let locale : Locale
250
+ let localeIdentifier : String
251
+ let localePreferences : LocalePreferences ?
250
252
}
251
253
252
254
fileprivate static let cache = FormatterCache < Signature , ICUNumberFormatter ? > ( )
253
255
254
256
private static func _create( with signature: Signature ) -> ICUNumberFormatter ? {
255
257
Self . cache. formatter ( for: signature) {
256
- . init( skeleton: signature. collection. skeleton, locale : signature. locale )
258
+ . init( skeleton: signature. collection. skeleton, localeIdentifier : signature. localeIdentifier , preferences : signature . localePreferences )
257
259
}
258
260
}
259
261
260
262
static func create< T: BinaryInteger > ( for style: IntegerFormatStyle < T > ) -> ICUNumberFormatter ? {
261
- _create ( with: . init( collection: style. collection, locale: style. locale) )
263
+ _create ( with: . init( collection: style. collection, localeIdentifier : style . locale. identifierCapturingPreferences , localePreferences : style. locale. prefs ) )
262
264
}
263
265
264
266
static func create( for style: Decimal . FormatStyle ) -> ICUNumberFormatter ? {
265
- _create ( with: . init( collection: style. collection, locale: style. locale) )
267
+ _create ( with: . init( collection: style. collection, localeIdentifier : style . locale. identifierCapturingPreferences , localePreferences : style. locale. prefs ) )
266
268
}
267
269
268
270
static func create< T: BinaryFloatingPoint > ( for style: FloatingPointFormatStyle < T > ) -> ICUNumberFormatter ? {
269
- _create ( with: . init( collection: style. collection, locale: style. locale) )
271
+ _create ( with: . init( collection: style. collection, localeIdentifier : style . locale. identifierCapturingPreferences , localePreferences : style. locale. prefs ) )
270
272
}
271
273
272
274
func attributedFormat( _ v: Value ) -> AttributedString {
@@ -283,7 +285,8 @@ final class ICUCurrencyNumberFormatter : ICUNumberFormatterBase {
283
285
fileprivate struct Signature : Hashable {
284
286
let collection : CurrencyFormatStyleConfiguration . Collection
285
287
let currencyCode : String
286
- let locale : Locale
288
+ let localeIdentifier : String
289
+ let localePreferences : LocalePreferences ?
287
290
}
288
291
289
292
private static func skeleton( for signature: Signature ) -> String {
@@ -301,20 +304,20 @@ final class ICUCurrencyNumberFormatter : ICUNumberFormatterBase {
301
304
302
305
static private func _create( with signature: Signature ) -> ICUCurrencyNumberFormatter ? {
303
306
return Self . cache. formatter ( for: signature) {
304
- . init( skeleton: Self . skeleton ( for: signature) , locale : signature. locale )
307
+ . init( skeleton: Self . skeleton ( for: signature) , localeIdentifier : signature. localeIdentifier , preferences : signature . localePreferences )
305
308
}
306
309
}
307
310
308
311
static func create< T: BinaryInteger > ( for style: IntegerFormatStyle < T > . Currency ) -> ICUCurrencyNumberFormatter ? {
309
- _create ( with: . init( collection: style. collection, currencyCode: style. currencyCode, locale: style. locale) )
312
+ _create ( with: . init( collection: style. collection, currencyCode: style. currencyCode, localeIdentifier : style . locale. identifierCapturingPreferences , localePreferences : style. locale. prefs ) )
310
313
}
311
314
312
315
static func create( for style: Decimal . FormatStyle . Currency ) -> ICUCurrencyNumberFormatter ? {
313
- _create ( with: . init( collection: style. collection, currencyCode: style. currencyCode, locale: style. locale) )
316
+ _create ( with: . init( collection: style. collection, currencyCode: style. currencyCode, localeIdentifier : style . locale. identifierCapturingPreferences , localePreferences : style. locale. prefs ) )
314
317
}
315
318
316
319
static func create< T: BinaryFloatingPoint > ( for style: FloatingPointFormatStyle < T > . Currency ) -> ICUCurrencyNumberFormatter ? {
317
- _create ( with: . init( collection: style. collection, currencyCode: style. currencyCode, locale: style. locale) )
320
+ _create ( with: . init( collection: style. collection, currencyCode: style. currencyCode, localeIdentifier : style . locale. identifierCapturingPreferences , localePreferences : style. locale. prefs ) )
318
321
}
319
322
320
323
func attributedFormat( _ v: Value ) -> AttributedString {
@@ -330,7 +333,8 @@ final class ICUCurrencyNumberFormatter : ICUNumberFormatterBase {
330
333
final class ICUPercentNumberFormatter : ICUNumberFormatterBase {
331
334
fileprivate struct Signature : Hashable {
332
335
let collection : NumberFormatStyleConfiguration . Collection
333
- let locale : Locale
336
+ let localeIdentifier : String
337
+ let localePreferences : LocalePreferences ?
334
338
}
335
339
336
340
private static func skeleton( for signature: Signature ) -> String {
@@ -346,20 +350,20 @@ final class ICUPercentNumberFormatter : ICUNumberFormatterBase {
346
350
347
351
private static func _create( with signature: Signature ) -> ICUPercentNumberFormatter ? {
348
352
return Self . cache. formatter ( for: signature) {
349
- . init( skeleton: Self . skeleton ( for: signature) , locale : signature. locale )
353
+ . init( skeleton: Self . skeleton ( for: signature) , localeIdentifier : signature. localeIdentifier , preferences : signature . localePreferences )
350
354
}
351
355
}
352
356
353
357
static func create< T: BinaryInteger > ( for style: IntegerFormatStyle < T > . Percent ) -> ICUPercentNumberFormatter ? {
354
- _create ( with: . init( collection: style. collection, locale: style. locale) )
358
+ _create ( with: . init( collection: style. collection, localeIdentifier : style . locale. identifierCapturingPreferences , localePreferences : style. locale. prefs ) )
355
359
}
356
360
357
361
static func create( for style: Decimal . FormatStyle . Percent ) -> ICUPercentNumberFormatter ? {
358
- _create ( with: . init( collection: style. collection, locale: style. locale) )
362
+ _create ( with: . init( collection: style. collection, localeIdentifier : style . locale. identifierCapturingPreferences , localePreferences : style. locale. prefs ) )
359
363
}
360
364
361
365
static func create< T: BinaryFloatingPoint > ( for style: FloatingPointFormatStyle < T > . Percent ) -> ICUPercentNumberFormatter ? {
362
- _create ( with: . init( collection: style. collection, locale: style. locale) )
366
+ _create ( with: . init( collection: style. collection, localeIdentifier : style . locale. identifierCapturingPreferences , localePreferences : style. locale. prefs ) )
363
367
}
364
368
365
369
func attributedFormat( _ v: Value ) -> AttributedString {
@@ -375,15 +379,16 @@ final class ICUPercentNumberFormatter : ICUNumberFormatterBase {
375
379
final class ICUByteCountNumberFormatter : ICUNumberFormatterBase {
376
380
fileprivate struct Signature : Hashable {
377
381
let skeleton : String
378
- let locale : Locale
382
+ let localeIdentifier : String
383
+ let localePreferences : LocalePreferences ?
379
384
}
380
385
381
386
fileprivate static let cache = FormatterCache < Signature , ICUByteCountNumberFormatter ? > ( )
382
387
383
388
static func create( for skeleton: String , locale: Locale ) -> ICUByteCountNumberFormatter ? {
384
- let signature = Signature ( skeleton: skeleton, locale: locale)
389
+ let signature = Signature ( skeleton: skeleton, localeIdentifier : locale. identifierCapturingPreferences , localePreferences : locale. prefs )
385
390
return Self . cache. formatter ( for: signature) {
386
- . init( skeleton: skeleton, locale: locale)
391
+ . init( skeleton: skeleton, localeIdentifier : locale. identifierCapturingPreferences , preferences : locale. prefs )
387
392
}
388
393
}
389
394
@@ -433,15 +438,16 @@ final class ICUByteCountNumberFormatter : ICUNumberFormatterBase {
433
438
final class ICUMeasurementNumberFormatter : ICUNumberFormatterBase {
434
439
fileprivate struct Signature : Hashable {
435
440
let skeleton : String
436
- let locale : Locale
441
+ let localeIdentifier : String
442
+ let localePreferences : LocalePreferences ?
437
443
}
438
444
439
445
fileprivate static let cache = FormatterCache < Signature , ICUMeasurementNumberFormatter ? > ( )
440
446
441
447
static func create( for skeleton: String , locale: Locale ) -> ICUMeasurementNumberFormatter ? {
442
- let signature = Signature ( skeleton: skeleton, locale: locale)
448
+ let signature = Signature ( skeleton: skeleton, localeIdentifier : locale. identifierCapturingPreferences , localePreferences : locale. prefs )
443
449
return Self . cache. formatter ( for: signature) {
444
- . init( skeleton: skeleton, locale: locale)
450
+ . init( skeleton: skeleton, localeIdentifier : locale. identifierCapturingPreferences , preferences : locale. prefs )
445
451
}
446
452
}
447
453
0 commit comments