-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
pal_locale.m
737 lines (668 loc) · 26.1 KB
/
pal_locale.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include <stdlib.h>
#include <string.h>
#include "pal_locale_internal.h"
#include "pal_localeStringData.h"
#include "pal_localeNumberData.h"
#import <Foundation/Foundation.h>
#import <Foundation/NSFormatter.h>
#if !__has_feature(objc_arc)
#error This file relies on ARC for memory management, but ARC is not enabled.
#endif
char* DetectDefaultAppleLocaleName(void)
{
NSLocale *currentLocale = [NSLocale currentLocale];
NSString *localeName = @"";
if (!currentLocale)
{
return strdup([localeName UTF8String]);
}
if ([currentLocale.languageCode length] > 0 && [currentLocale.countryCode length] > 0)
{
localeName = [NSString stringWithFormat:@"%@-%@", currentLocale.languageCode, currentLocale.countryCode];
}
else
{
localeName = currentLocale.localeIdentifier;
}
return strdup([localeName UTF8String]);
}
#if defined(TARGET_MACCATALYST) || defined(TARGET_IOS) || defined(TARGET_TVOS)
const char* GlobalizationNative_GetLocaleNameNative(const char* localeName)
{
@autoreleasepool
{
NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];
const char* value = [currentLocale.localeIdentifier UTF8String];
return strdup(value);
}
}
/**
* Useful constant for the maximum size of the whole locale ID
* (including the terminating NULL and all keywords).
*/
#define FULLNAME_CAPACITY 157
static void GetParent(const char* localeID, char* parent, int32_t parentCapacity)
{
const char *lastUnderscore;
int32_t i;
if (localeID == NULL)
localeID = [NSLocale systemLocale].localeIdentifier.UTF8String;
lastUnderscore = strrchr(localeID, '-');
if (lastUnderscore != NULL)
{
i = (int32_t)(lastUnderscore - localeID);
}
else
{
i = 0;
}
if (i > 0)
{
// primary lang subtag und (undefined).
if (strncasecmp(localeID, "und-", 4) == 0)
{
localeID += 3;
i -= 3;
memmove(parent, localeID, MIN(i, parentCapacity));
}
else if (parent != localeID)
{
memcpy(parent, localeID, MIN(i, parentCapacity));
}
}
// terminate chars
if (i >= 0 && i < parentCapacity)
parent[i] = 0;
}
const char* GlobalizationNative_GetLocaleInfoStringNative(const char* localeName, LocaleStringData localeStringData)
{
@autoreleasepool
{
NSString *value;
NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.locale = currentLocale;
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:currentLocale];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
switch (localeStringData)
{
///// <summary>localized name of locale, eg "German (Germany)" in UI language (corresponds to LOCALE_SLOCALIZEDDISPLAYNAME)</summary>
case LocaleString_LocalizedDisplayName:
/// <summary>Display name (language + country usually) in English, eg "German (Germany)" (corresponds to LOCALE_SENGLISHDISPLAYNAME)</summary>
case LocaleString_EnglishDisplayName:
value = [gbLocale displayNameForKey:NSLocaleIdentifier value:currentLocale.localeIdentifier];
break;
/// <summary>Display name in native locale language, eg "Deutsch (Deutschland) (corresponds to LOCALE_SNATIVEDISPLAYNAME)</summary>
case LocaleString_NativeDisplayName:
value = [currentLocale displayNameForKey:NSLocaleIdentifier value:currentLocale.localeIdentifier];
break;
/// <summary>Language Display Name for a language, eg "German" in UI language (corresponds to LOCALE_SLOCALIZEDLANGUAGENAME)</summary>
case LocaleString_LocalizedLanguageName:
/// <summary>English name of language, eg "German" (corresponds to LOCALE_SENGLISHLANGUAGENAME)</summary>
case LocaleString_EnglishLanguageName:
value = [gbLocale localizedStringForLanguageCode:currentLocale.languageCode];
break;
/// <summary>native name of language, eg "Deutsch" (corresponds to LOCALE_SNATIVELANGUAGENAME)</summary>
case LocaleString_NativeLanguageName:
value = [currentLocale localizedStringForLanguageCode:currentLocale.languageCode];
break;
/// <summary>English name of country, eg "Germany" (corresponds to LOCALE_SENGLISHCOUNTRYNAME)</summary>
case LocaleString_EnglishCountryName:
value = [gbLocale localizedStringForCountryCode:currentLocale.countryCode];
break;
/// <summary>native name of country, eg "Deutschland" (corresponds to LOCALE_SNATIVECOUNTRYNAME)</summary>
case LocaleString_NativeCountryName:
value = [currentLocale localizedStringForCountryCode:currentLocale.countryCode];
break;
case LocaleString_ThousandSeparator:
value = currentLocale.groupingSeparator;
break;
case LocaleString_DecimalSeparator:
value = currentLocale.decimalSeparator;
// or value = [[currentLocale objectForKey:NSLocaleDecimalSeparator] UTF8String];
break;
case LocaleString_Digits:
{
NSString *digitsString = @"0123456789";
NSNumberFormatter *nf1 = [[NSNumberFormatter alloc] init];
[nf1 setLocale:currentLocale];
NSNumber *newNum = [nf1 numberFromString:digitsString];
value = [newNum stringValue];
break;
}
case LocaleString_MonetarySymbol:
value = currentLocale.currencySymbol;
break;
case LocaleString_Iso4217MonetarySymbol:
// check if this is correct, check currencyISOCode
value = currentLocale.currencyCode;
break;
case LocaleString_CurrencyEnglishName:
value = [gbLocale localizedStringForCurrencyCode:currentLocale.currencyCode];
break;
case LocaleString_CurrencyNativeName:
value = [currentLocale localizedStringForCurrencyCode:currentLocale.currencyCode];
break;
case LocaleString_MonetaryDecimalSeparator:
value = numberFormatter.currencyDecimalSeparator;
break;
case LocaleString_MonetaryThousandSeparator:
value = numberFormatter.currencyGroupingSeparator;
break;
case LocaleString_AMDesignator:
value = dateFormatter.AMSymbol;
break;
case LocaleString_PMDesignator:
value = dateFormatter.PMSymbol;
break;
case LocaleString_PositiveSign:
value = numberFormatter.plusSign;
break;
case LocaleString_NegativeSign:
value = numberFormatter.minusSign;
break;
case LocaleString_Iso639LanguageTwoLetterName:
value = [currentLocale objectForKey:NSLocaleLanguageCode];
break;
case LocaleString_Iso639LanguageThreeLetterName:
{
NSString *iso639_2 = [currentLocale objectForKey:NSLocaleLanguageCode];
return iso639_2 == nil ? strdup("") : strdup(uloc_getISO3LanguageByLangCode([iso639_2 UTF8String]));
}
case LocaleString_Iso3166CountryName:
value = [currentLocale objectForKey:NSLocaleCountryCode];
break;
case LocaleString_Iso3166CountryName2:
{
NSString* countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
return countryCode == nil ? strdup("") : strdup(uloc_getISO3CountryByCountryCode([countryCode UTF8String]));
}
case LocaleString_NaNSymbol:
value = numberFormatter.notANumberSymbol;
break;
case LocaleString_PositiveInfinitySymbol:
value = numberFormatter.positiveInfinitySymbol;
break;
case LocaleString_NegativeInfinitySymbol:
value = numberFormatter.negativeInfinitySymbol;
break;
case LocaleString_PercentSymbol:
value = numberFormatter.percentSymbol;
break;
case LocaleString_PerMilleSymbol:
value = numberFormatter.perMillSymbol;
break;
case LocaleString_ParentName:
{
char localeNameTemp[FULLNAME_CAPACITY];
const char* lName = [currentLocale.localeIdentifier UTF8String];
GetParent(lName, localeNameTemp, FULLNAME_CAPACITY);
return strdup(localeNameTemp);
}
default:
value = nil;
break;
}
return value == nil ? strdup("") : strdup([value UTF8String]);
}
}
// invariant character definitions
#define CHAR_CURRENCY ((char)0x00A4) // international currency
#define CHAR_SPACE ((char)0x0020) // space
#define CHAR_NBSPACE ((char)0x00A0) // no-break space
#define CHAR_DIGIT ((char)0x0023) // '#'
#define CHAR_MINUS ((char)0x002D) // '-'
#define CHAR_PERCENT ((char)0x0025) // '%'
#define CHAR_OPENPAREN ((char)0x0028) // '('
#define CHAR_CLOSEPAREN ((char)0x0029) // ')'
#define CHAR_ZERO ((char)0x0030) // '0'
/*
Function:
NormalizeNumericPattern
Returns a numeric string pattern in a format that we can match against the
appropriate managed pattern. Examples:
For PositiveMonetaryNumberFormat "¤#,##0.00" becomes "Cn"
For NegativeNumberFormat "#,##0.00;(#,##0.00)" becomes "(n)"
*/
static char* NormalizeNumericPattern(const char* srcPattern, int isNegative)
{
int iStart = 0;
int iEnd = strlen(srcPattern);
// ';' separates positive and negative subpatterns.
// When there is no explicit negative subpattern,
// an implicit negative subpattern is formed from the positive pattern with a prefixed '-'.
char * ptrNegativePattern = strrchr(srcPattern,';');
if (ptrNegativePattern)
{
int32_t iNegativePatternStart = ptrNegativePattern - srcPattern;
if (isNegative)
{
iStart = iNegativePatternStart + 1;
}
else
{
iEnd = iNegativePatternStart - 1;
}
}
int minusAdded = false;
for (int i = iStart; i <= iEnd; i++)
{
switch (srcPattern[i])
{
case CHAR_MINUS:
case CHAR_OPENPAREN:
case CHAR_CLOSEPAREN:
minusAdded = true;
break;
}
if (minusAdded)
break;
}
// international currency symbol (CHAR_CURRENCY)
// The positive pattern comes first, then an optional negative pattern
// separated by a semicolon
// A destPattern example: "(C n)" where C represents the currency symbol, and
// n is the number
char* destPattern;
int index = 0;
// if there is no negative subpattern, prefix the minus sign
if (isNegative && !minusAdded)
{
int length = (iEnd - iStart) + 2;
destPattern = (char*)calloc((size_t)length, sizeof(char));
if (!destPattern)
{
return NULL;
}
destPattern[index++] = '-';
}
else
{
int length = (iEnd - iStart) + 1;
destPattern = (char*)calloc((size_t)length, sizeof(char));
if (!destPattern)
{
return NULL;
}
}
int digitAdded = false;
int currencyAdded = false;
int spaceAdded = false;
for (int i = iStart; i <= iEnd; i++)
{
char ch = srcPattern[i];
switch (ch)
{
case CHAR_DIGIT:
case CHAR_ZERO:
if (!digitAdded)
{
digitAdded = true;
destPattern[index++] = 'n';
}
break;
case CHAR_CURRENCY:
if (!currencyAdded)
{
currencyAdded = true;
destPattern[index++] = 'C';
}
break;
case CHAR_SPACE:
case CHAR_NBSPACE:
if (!spaceAdded)
{
spaceAdded = true;
destPattern[index++] = ' ';
}
break;
case CHAR_MINUS:
case CHAR_OPENPAREN:
case CHAR_CLOSEPAREN:
case CHAR_PERCENT:
destPattern[index++] = ch;
break;
}
}
const int MAX_DOTNET_NUMERIC_PATTERN_LENGTH = 6; // example: "(C n)" plus terminator
if (destPattern[0] == '\0' || strlen (destPattern) >= MAX_DOTNET_NUMERIC_PATTERN_LENGTH)
{
free (destPattern);
return NULL;
}
return destPattern;
}
/*
Function:
GetNumericPattern
Determines the pattern from the decimalFormat and returns the matching pattern's
index from patterns[].
Returns index -1 if no pattern is found.
*/
static int GetPatternIndex(char* normalizedPattern,const char* patterns[], int patternsCount)
{
const int INVALID_FORMAT = -1;
if (!normalizedPattern)
{
return INVALID_FORMAT;
}
for (int i = 0; i < patternsCount; i++)
{
if (strcmp(normalizedPattern, patterns[i]) == 0)
{
free(normalizedPattern);
return i;
}
}
assert(false); // should have found a valid pattern
free(normalizedPattern);
return INVALID_FORMAT;
}
static int32_t GetValueForNumberFormat(NSLocale *currentLocale, LocaleNumberData localeNumberData)
{
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.locale = currentLocale;
const char *pFormat;
int32_t value;
switch(localeNumberData)
{
case LocaleNumber_PositiveMonetaryNumberFormat:
{
numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
static const char* Patterns[] = {"Cn", "nC", "C n", "n C"};
pFormat = [[numberFormatter positiveFormat] UTF8String];
char* normalizedPattern = NormalizeNumericPattern(pFormat, false);
value = GetPatternIndex(normalizedPattern, Patterns, sizeof(Patterns)/sizeof(Patterns[0]));
break;
}
case LocaleNumber_NegativeMonetaryNumberFormat:
{
numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
static const char* Patterns[] = {"(Cn)", "-Cn", "C-n", "Cn-", "(nC)", "-nC", "n-C", "nC-", "-n C",
"-C n", "n C-", "C n-", "C -n", "n- C", "(C n)", "(n C)", "C- n" };
pFormat = [[numberFormatter negativeFormat] UTF8String];
char* normalizedPattern = NormalizeNumericPattern(pFormat, true);
value = GetPatternIndex(normalizedPattern, Patterns, sizeof(Patterns)/sizeof(Patterns[0]));
break;
}
case LocaleNumber_NegativeNumberFormat:
{
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
static const char* Patterns[] = {"(n)", "-n", "- n", "n-", "n -"};
pFormat = [[numberFormatter negativeFormat] UTF8String];
char* normalizedPattern = NormalizeNumericPattern(pFormat, true);
value = GetPatternIndex(normalizedPattern, Patterns, sizeof(Patterns)/sizeof(Patterns[0]));
break;
}
case LocaleNumber_NegativePercentFormat:
{
numberFormatter.numberStyle = NSNumberFormatterPercentStyle;
static const char* Patterns[] = {"-n %", "-n%", "-%n", "%-n", "%n-", "n-%", "n%-", "-% n", "n %-", "% n-", "% -n", "n- %"};
pFormat = [[numberFormatter negativeFormat] UTF8String];
char* normalizedPattern = NormalizeNumericPattern(pFormat, true);
value = GetPatternIndex(normalizedPattern, Patterns, sizeof(Patterns)/sizeof(Patterns[0]));
break;
}
case LocaleNumber_PositivePercentFormat:
{
numberFormatter.numberStyle = NSNumberFormatterPercentStyle;
static const char* Patterns[] = {"n %", "n%", "%n", "% n"};
pFormat = [[numberFormatter positiveFormat] UTF8String];
char* normalizedPattern = NormalizeNumericPattern(pFormat, false);
value = GetPatternIndex(normalizedPattern, Patterns, sizeof(Patterns)/sizeof(Patterns[0]));
break;
}
default:
return -1;
}
return value;
}
int32_t GlobalizationNative_GetLocaleInfoIntNative(const char* localeName, LocaleNumberData localeNumberData)
{
@autoreleasepool
{
#ifndef NDEBUG
bool isSuccess = true;
#endif
int32_t value;
NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];
switch (localeNumberData)
{
case LocaleNumber_MeasurementSystem:
{
const char *measurementSystem = [[currentLocale objectForKey:NSLocaleMeasurementSystem] UTF8String];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
const char *us_measurementSystem = [[usLocale objectForKey:NSLocaleMeasurementSystem] UTF8String];
value = (measurementSystem == us_measurementSystem) ? 1 : 0;
break;
}
case LocaleNumber_FractionalDigitsCount:
{
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.locale = currentLocale;
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
value = (int32_t)numberFormatter.maximumFractionDigits;
break;
}
case LocaleNumber_MonetaryFractionalDigitsCount:
{
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.locale = currentLocale;
numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
value = (int32_t)numberFormatter.maximumFractionDigits;
break;
}
case LocaleNumber_PositiveMonetaryNumberFormat:
case LocaleNumber_NegativeMonetaryNumberFormat:
case LocaleNumber_NegativeNumberFormat:
case LocaleNumber_NegativePercentFormat:
case LocaleNumber_PositivePercentFormat:
{
value = GetValueForNumberFormat(currentLocale, localeNumberData);
#ifndef NDEBUG
if (value < 0)
{
isSuccess = false;
}
#endif
break;
}
case LocaleNumber_FirstWeekOfYear:
{
NSCalendar *calendar = [currentLocale objectForKey:NSLocaleCalendar];
int minDaysInWeek = (int32_t)[calendar minimumDaysInFirstWeek];
if (minDaysInWeek == 1)
{
value = WeekRule_FirstDay;
}
else if (minDaysInWeek == 7)
{
value = WeekRule_FirstFullWeek;
}
else if (minDaysInWeek >= 4)
{
value = WeekRule_FirstFourDayWeek;
}
else
{
value = -1;
#ifndef NDEBUG
isSuccess = false;
#endif
}
break;
}
case LocaleNumber_ReadingLayout:
{
NSLocaleLanguageDirection langDir = [NSLocale characterDirectionForLanguage:[currentLocale objectForKey:NSLocaleLanguageCode]];
// 0 - Left to right (such as en-US)
// 1 - Right to left (such as arabic locales)
value = NSLocaleLanguageDirectionRightToLeft == langDir ? 1 : 0;
break;
}
case LocaleNumber_FirstDayofWeek:
{
NSCalendar *calendar = [currentLocale objectForKey:NSLocaleCalendar];
value = [calendar firstWeekday] - 1; // .NET is 0-based and in Apple is 1-based;
break;
}
default:
value = -1;
#ifndef NDEBUG
isSuccess = false;
#endif
break;
}
assert(isSuccess);
return value;
}
}
/*
PAL Function:
GlobalizationNative_GetLocaleInfoPrimaryGroupingSizeNative
Returns primary grouping size for decimal and currency
*/
int32_t GlobalizationNative_GetLocaleInfoPrimaryGroupingSizeNative(const char* localeName, LocaleNumberData localeGroupingData)
{
@autoreleasepool
{
NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.locale = currentLocale;
switch (localeGroupingData)
{
case LocaleNumber_Digit:
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
break;
case LocaleNumber_Monetary:
numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
break;
default:
assert(false);
break;
}
return [numberFormatter groupingSize];
}
}
/*
PAL Function:
GlobalizationNative_GetLocaleInfoSecondaryGroupingSizeNative
Returns secondary grouping size for decimal and currency
*/
int32_t GlobalizationNative_GetLocaleInfoSecondaryGroupingSizeNative(const char* localeName, LocaleNumberData localeGroupingData)
{
@autoreleasepool
{
NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.locale = currentLocale;
switch (localeGroupingData)
{
case LocaleNumber_Digit:
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
break;
case LocaleNumber_Monetary:
numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
break;
default:
assert(false);
break;
}
return [numberFormatter secondaryGroupingSize];
}
}
/*
PAL Function:
GlobalizationNative_GetLocaleTimeFormatNative
Returns time format information (in native format, it needs to be converted to .NET's format).
*/
const char* GlobalizationNative_GetLocaleTimeFormatNative(const char* localeName, int shortFormat)
{
@autoreleasepool
{
NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:currentLocale];
if (shortFormat != 0)
{
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
}
else
{
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
}
return strdup([[dateFormatter dateFormat] UTF8String]);
}
}
// GlobalizationNative_GetLocalesNative gets all locale names and store it in the value buffer
// in case of success, it returns the count of the characters stored in value buffer
// in case of failure, it returns negative number.
// if the input value buffer is null, it returns the length needed to store the
// locale names list.
// if the value is not null, it fills the value with locale names separated by the length
// of each name.
int32_t GlobalizationNative_GetLocalesNative(UChar* value, int32_t length)
{
@autoreleasepool
{
NSArray<NSString*>* availableLocaleIdentifiers = [NSLocale availableLocaleIdentifiers];
int32_t index = 0;
int32_t totalLength = 0;
int32_t availableLength = (int32_t)[availableLocaleIdentifiers count];
if (availableLength <= 0)
return -1; // failed
for (NSInteger i = 0; i < availableLength; i++)
{
NSString *localeIdentifier = availableLocaleIdentifiers[i];
int32_t localeNameLength = localeIdentifier.length;
totalLength += localeNameLength + 1; // add 1 for the name length
if (value != NULL)
{
if (totalLength > length)
return -3;
value[index++] = (UChar) localeNameLength;
for (int j = 0; j < localeNameLength; j++)
{
if ((UChar)[localeIdentifier characterAtIndex:j] == '_')
{
value[index++] = (UChar) '-';
}
else
{
value[index++] = (UChar) [localeIdentifier characterAtIndex:j];
}
}
}
}
return totalLength;
}
}
#endif
#if defined(TARGET_MACCATALYST) || defined(TARGET_IOS) || defined(TARGET_TVOS)
const char* GlobalizationNative_GetICUDataPathRelativeToAppBundleRoot(const char* path)
{
@autoreleasepool
{
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *dataPath = [bundlePath stringByAppendingPathComponent: [NSString stringWithFormat:@"%s", path]];
return strdup([dataPath UTF8String]);
}
}
const char* GlobalizationNative_GetICUDataPathFallback(void)
{
@autoreleasepool
{
NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"icudt" ofType:@"dat"];
return strdup([dataPath UTF8String]);
}
}
#endif