-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSLocale.m
More file actions
1110 lines (956 loc) · 28.5 KB
/
NSLocale.m
File metadata and controls
1110 lines (956 loc) · 28.5 KB
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* NSLocale.m
Copyright (C) 2010 Free Software Foundation, Inc.
Written by: Stefan Bidigaray
Date: June, 2010
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; see the file COPYING.LIB.
If not, see <http://www.gnu.org/licenses/> or write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#define EXPOSE_NSLocale_IVARS 1
#import "common.h"
#import "Foundation/NSLocale.h"
#import "Foundation/NSArray.h"
#import "Foundation/NSCalendar.h"
#import "Foundation/NSCoder.h"
#import "Foundation/NSCharacterSet.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSLock.h"
#import "Foundation/NSValue.h"
#import "Foundation/NSNotification.h"
#import "Foundation/NSNumberFormatter.h"
#import "Foundation/NSUserDefaults.h"
#import "Foundation/NSString.h"
#import "GNUstepBase/GSLock.h"
#if defined(HAVE_UNICODE_ULOC_H)
# include <unicode/uloc.h>
#endif
#if defined(HAVE_UNICODE_ULOCDATA_H)
# include <unicode/ulocdata.h>
#endif
#if defined(HAVE_UNICODE_UCURR_H)
# include <unicode/ucurr.h>
#endif
@interface NSLocale (PrivateMethods)
+ (void) _updateCanonicalLocales;
- (NSString *) _getMeasurementSystem;
- (NSCharacterSet *) _getExemplarCharacterSet;
- (NSString *) _getDelimiterWithType: (NSInteger) delimiterType;
- (NSCalendar *) _getCalendar;
- (NSString *) _getDecimalSeparator;
- (NSString *) _getGroupingSeparator;
- (NSString *) _getCurrencySymbol;
- (NSString *) _getCurrencyCode;
@end
#if GS_USE_ICU == 1
//
// ICU Component Keywords
//
static const char * ICUCalendarKeyword = "calendar";
static const char * ICUCollationKeyword = "collation";
static NSLocaleLanguageDirection
ICUToNSLocaleOrientation (ULayoutType layout)
{
switch (layout)
{
case ULOC_LAYOUT_LTR:
return NSLocaleLanguageDirectionLeftToRight;
case ULOC_LAYOUT_RTL:
return NSLocaleLanguageDirectionRightToLeft;
case ULOC_LAYOUT_TTB:
return NSLocaleLanguageDirectionTopToBottom;
case ULOC_LAYOUT_BTT:
return NSLocaleLanguageDirectionBottomToTop;
default:
return NSLocaleLanguageDirectionUnknown;
}
}
static NSArray *_currencyCodesWithType (uint32_t currType)
{
NSArray *result;
NSMutableArray *currencies;
UErrorCode err = U_ZERO_ERROR;
const char *currCode;
UEnumeration *codes;
codes = ucurr_openISOCurrencies (currType, &err);
if (U_FAILURE(err))
return nil;
currencies = [[NSMutableArray alloc] initWithCapacity: 10];
do
{
int strLength;
err = U_ZERO_ERROR;
currCode = uenum_next (codes, &strLength, &err);
if (U_FAILURE(err))
{
uenum_close (codes);
[currencies release];
return nil;
}
if (currCode == NULL)
break;
[currencies addObject: [NSString stringWithUTF8String: currCode]];
} while (NULL != currCode);
uenum_close (codes);
result = [NSArray arrayWithArray: currencies];
[currencies release];
return result;
}
#endif
@implementation NSLocale
static NSLocale *autoupdatingLocale = nil;
static NSLocale *currentLocale = nil;
static NSLocale *systemLocale = nil;
static NSMutableDictionary *allLocales = nil;
static NSDictionary *canonicalLocales = nil;
static NSRecursiveLock *classLock = nil;
+ (void) initialize
{
if (self == [NSLocale class])
{
classLock = [NSRecursiveLock new];
[[NSObject leakAt: &classLock] release];
allLocales = [[NSMutableDictionary alloc] initWithCapacity: 0];
[[NSObject leakAt: &allLocales] release];
}
}
+ (void) defaultsDidChange: (NSNotification*)n
{
NSUserDefaults *defs;
NSString *name;
defs = [NSUserDefaults standardUserDefaults];
name = [defs stringForKey: @"Locale"];
if ([name isEqual: autoupdatingLocale->_localeId] == NO)
{
[classLock lock];
RELEASE(autoupdatingLocale->_localeId);
RELEASE(autoupdatingLocale->_components);
autoupdatingLocale->_localeId = RETAIN(name);
autoupdatingLocale->_components = nil;
RELEASE(currentLocale);
currentLocale = nil;
[classLock unlock];
[[NSNotificationCenter defaultCenter]
postNotificationName: NSCurrentLocaleDidChangeNotification
object: nil];
}
}
+ (id) autoupdatingCurrentLocale
{
NSLocale *result;
[classLock lock];
if (nil == autoupdatingLocale)
{
autoupdatingLocale = [[self currentLocale] copy];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(defaultsDidChange:)
name: NSUserDefaultsDidChangeNotification
object: nil];
}
result = RETAIN(autoupdatingLocale);
[classLock unlock];
return AUTORELEASE(result);
}
+ (NSArray *) availableLocaleIdentifiers
{
static NSArray *available = nil;
#if GS_USE_ICU == 1
if (nil == available)
{
[classLock lock];
if (nil == available)
{
NSMutableArray *array;
int32_t i;
int32_t count = uloc_countAvailable ();
array = [[NSMutableArray alloc] initWithCapacity: count];
for (i = 0; i < count; ++i)
{
const char *localeID = uloc_getAvailable (i);
[array addObject: [NSString stringWithUTF8String: localeID]];
}
available = [[NSArray alloc] initWithArray: array];
[array release];
}
[classLock unlock];
}
#endif
return [[available copy] autorelease];
}
+ (NSString *) canonicalLanguageIdentifierFromString: (NSString *) string
{
NSString *result;
NSString *localeId;
NSArray *localeComps;
/* Can't use the ICU functions here because, according to Apple locale docs,
the language has a format like "zh-Hant". ICU, however, uses an
underscore to separate Scripts "zh_Hant". */
if (canonicalLocales == nil)
[self _updateCanonicalLocales];
localeId = [canonicalLocales objectForKey: string];
if (nil == localeId)
{
result = string;
}
else
{
localeComps = [localeId componentsSeparatedByString: @"_"];
result = [localeComps objectAtIndex: 0];
}
return result;
}
+ (NSString *) canonicalLocaleIdentifierFromString: (NSString *) string
{
/* The way this works, according to Apple docs, is a mess. It seems
that both BCP 47's "-" and ICU's "_" separators are used. According to
"Language and Locale Designations" (Apple docs) Taiwan, for example, has
zh-Hant_TW as it's locale identifier (was zh_TW on 10.3.9 and below).
Since ICU doesn't use "-" as a separator it will modify that identifier
to zh_Hant_TW. */
NSString *result;
NSMutableString *mStr;
NSRange range;
if (string == nil)
return nil;
if (canonicalLocales == nil)
[self _updateCanonicalLocales];
result = [canonicalLocales objectForKey: string];
if (result == nil)
result = string;
// Strip script info from locale
range = [result rangeOfString: @"-"];
if (range.location != NSNotFound)
{
NSUInteger start = range.location;
NSUInteger length;
range = [result rangeOfString: @"_"];
length = range.location - start;
mStr = [NSMutableString stringWithString: result];
[mStr deleteCharactersInRange: NSMakeRange (start, length)];
result = [NSString stringWithString: mStr];
}
return result;
}
+ (NSLocaleLanguageDirection) characterDirectionForLanguage:
(NSString *)isoLangCode
{
#if GS_USE_ICU == 1
ULayoutType result;
UErrorCode status = U_ZERO_ERROR;
result = uloc_getCharacterOrientation ([isoLangCode UTF8String], &status);
if (U_FAILURE(status) || ULOC_LAYOUT_UNKNOWN == result)
return NSLocaleLanguageDirectionUnknown;
return ICUToNSLocaleOrientation (result);
#else
return NSLocaleLanguageDirectionLeftToRight; // FIXME
#endif
}
+ (NSDictionary *) componentsFromLocaleIdentifier: (NSString *) string
{
#if GS_USE_ICU == 1
char buffer[ULOC_KEYWORD_AND_VALUES_CAPACITY];
const char *cLocaleId = [string UTF8String];
int32_t strLength;
UEnumeration *enumerator;
UErrorCode error = U_ZERO_ERROR;
NSDictionary *result;
NSMutableDictionary *tmpDict =
[[NSMutableDictionary alloc] initWithCapacity: 5];
strLength = uloc_getLanguage (cLocaleId, buffer,
ULOC_KEYWORD_AND_VALUES_CAPACITY, &error);
if (U_SUCCESS(error) && strLength)
{
[tmpDict setValue: [NSString stringWithUTF8String: buffer]
forKey: NSLocaleLanguageCode];
}
error = U_ZERO_ERROR;
strLength = uloc_getCountry (cLocaleId, buffer,
ULOC_KEYWORD_AND_VALUES_CAPACITY, &error);
if (U_SUCCESS(error) && strLength)
{
[tmpDict setValue: [NSString stringWithUTF8String: buffer]
forKey: NSLocaleCountryCode];
}
error = U_ZERO_ERROR;
strLength = uloc_getScript (cLocaleId, buffer,
ULOC_KEYWORD_AND_VALUES_CAPACITY, &error);
if (U_SUCCESS(error) && strLength)
{
[tmpDict setValue: [NSString stringWithUTF8String: buffer]
forKey: NSLocaleScriptCode];
}
error = U_ZERO_ERROR;
strLength = uloc_getVariant (cLocaleId, buffer,
ULOC_KEYWORD_AND_VALUES_CAPACITY, &error);
if (U_SUCCESS(error) && strLength)
{
[tmpDict setValue: [NSString stringWithUTF8String: buffer]
forKey: NSLocaleVariantCode];
}
error = U_ZERO_ERROR;
enumerator = uloc_openKeywords (cLocaleId, &error);
if (U_SUCCESS(error))
{
const char *keyword;
error = U_ZERO_ERROR;
keyword = uenum_next(enumerator, NULL, &error);
while (keyword && U_SUCCESS(error))
{
error = U_ZERO_ERROR;
strLength = uloc_getKeywordValue (cLocaleId, keyword, buffer,
ULOC_KEYWORD_AND_VALUES_CAPACITY, &error);
if (strLength && U_SUCCESS(error))
{
// This is OK because NSLocaleCalendarIdentifier = "calendar"
// and NSLocaleCollationIdentifier = "collation".
[tmpDict setValue: [NSString stringWithUTF8String: buffer]
forKey: [NSString stringWithUTF8String: keyword]];
error = U_ZERO_ERROR;
keyword = uenum_next (enumerator, NULL, &error);
}
}
}
uenum_close (enumerator);
result = [NSDictionary dictionaryWithDictionary: tmpDict];
RELEASE(tmpDict);
return result;
#else
return nil; // FIXME
#endif
}
+ (id) currentLocale
{
NSLocale *result;
[classLock lock];
if (nil == currentLocale)
{
NSString *localeId;
[classLock unlock];
localeId =
[[NSUserDefaults standardUserDefaults] objectForKey: @"Locale"];
[classLock lock];
if (currentLocale == nil)
currentLocale = [[NSLocale alloc] initWithLocaleIdentifier: localeId];
}
result = RETAIN(currentLocale);
[classLock unlock];
return AUTORELEASE(result);
}
+ (NSArray *) commonISOCurrencyCodes
{
#if GS_USE_ICU == 1
return _currencyCodesWithType (UCURR_COMMON | UCURR_NON_DEPRECATED);
#else
return nil; // FIXME
#endif
}
+ (NSArray *) ISOCurrencyCodes
{
#if GS_USE_ICU == 1
return _currencyCodesWithType (UCURR_ALL);
#else
return nil; // FIXME
#endif
}
+ (NSArray *) ISOCountryCodes
{
static NSArray *countries = nil;
if (nil == countries)
{
#if GS_USE_ICU == 1
[classLock lock];
if (nil == countries)
{
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity: 10];
const char *const *codes = uloc_getISOCountries ();
while (*codes != NULL)
{
[array addObject: [NSString stringWithUTF8String: *codes]];
++codes;
}
countries = [[NSArray alloc] initWithArray: array];
[array release];
}
[classLock unlock];
#endif
}
return [[countries copy] autorelease];
}
+ (NSArray *) ISOLanguageCodes
{
static NSArray *languages = nil;
if (nil == languages)
{
#if GS_USE_ICU == 1
[classLock lock];
if (nil == languages)
{
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity: 10];
const char *const *codes = uloc_getISOLanguages ();
while (*codes != NULL)
{
[array addObject: [NSString stringWithUTF8String: *codes]];
++codes;
}
languages = [[NSArray alloc] initWithArray: array];
[array release];
}
[classLock unlock];
#endif
}
return [[languages copy] autorelease];
}
+ (NSLocaleLanguageDirection) lineDirectionForLanguage: (NSString *) isoLangCode
{
#if GS_USE_ICU == 1
ULayoutType result;
UErrorCode status = U_ZERO_ERROR;
result = uloc_getLineOrientation ([isoLangCode UTF8String], &status);
if (U_FAILURE(status) || ULOC_LAYOUT_UNKNOWN == result)
return NSLocaleLanguageDirectionUnknown;
return ICUToNSLocaleOrientation (result);
#else
return NSLocaleLanguageDirectionTopToBottom; // FIXME
#endif
}
+ (NSArray *) preferredLanguages
{
NSArray *result;
NSMutableArray *mArray;
NSUInteger cnt;
NSUInteger idx = 0;
NSArray *languages;
languages = [[NSUserDefaults standardUserDefaults]
stringArrayForKey: @"NSLanguages"];
if (languages == nil)
return [NSArray arrayWithObject: @"en"];
mArray = [NSMutableArray array];
cnt = [languages count];
while (idx < cnt)
{
NSString *lang = [self canonicalLanguageIdentifierFromString:
[languages objectAtIndex: idx]];
if (![mArray containsObject: lang])
[mArray addObject: lang];
++idx;
}
result = [NSArray arrayWithArray: mArray];
return result;
}
+ (id) systemLocale
{
NSLocale *result;
[classLock lock];
if (nil == systemLocale)
{
#if GS_USE_ICU == 1
systemLocale = [[NSLocale alloc] initWithLocaleIdentifier: @""];
#endif
}
result = RETAIN(systemLocale);
[classLock unlock];
return AUTORELEASE(result);
}
+ (id) localeWithLocaleIdentifier:(NSString *)string
{
return AUTORELEASE([[NSLocale alloc] initWithLocaleIdentifier: string]);
}
+ (NSString *) localeIdentifierFromComponents: (NSDictionary *) dict
{
NSString *result;
NSMutableString *string;
const char *language = [[dict objectForKey: NSLocaleLanguageCode] UTF8String];
const char *script = [[dict objectForKey: NSLocaleScriptCode] UTF8String];
const char *country = [[dict objectForKey: NSLocaleCountryCode] UTF8String];
const char *variant = [[dict objectForKey: NSLocaleVariantCode] UTF8String];
const char *calendar =
[[[dict objectForKey: NSLocaleCalendar] calendarIdentifier] UTF8String];
const char *collation =
[[dict objectForKey: NSLocaleCollationIdentifier] UTF8String];
const char *currency = [[dict objectForKey: NSLocaleCurrencyCode] UTF8String];
if (!calendar)
{
calendar = [[dict objectForKey: NSLocaleCalendarIdentifier] UTF8String];
}
// A locale cannot be constructed without a language.
if (language == NULL)
return nil;
#define TEST_CODE(x) (x ? "_" : ""), (x ? x : "")
string = [[NSMutableString alloc] initWithFormat: @"%s%s%s%s%s%s%s",
language, TEST_CODE(script), TEST_CODE(country), TEST_CODE(variant)];
#undef TEST_CODE
// I'm not using uloc_setKeywordValue() here because the format is easy
// enough to reproduce and has the added advatange that we doesn't need ICU.
if (calendar || collation || currency)
[string appendString: @"@"];
if (calendar)
[string appendFormat: @"calendar=%s", calendar];
if (collation)
{
if (calendar)
[string appendString: @";"];
[string appendFormat: @"collation=%s", collation];
}
if (currency)
{
if (calendar || currency)
[string appendString: @";"];
[string appendFormat: @"currency=%s", currency];
}
result = [NSString stringWithString: string];
RELEASE(string);
return result;
}
+ (NSString *) localeIdentifierFromWindowsLocaleCode: (uint32_t) lcid
{
#if GS_USE_ICU == 1
char buffer[ULOC_FULLNAME_CAPACITY];
UErrorCode status = U_ZERO_ERROR;
uloc_getLocaleForLCID (lcid, buffer, ULOC_FULLNAME_CAPACITY, &status);
if (U_FAILURE(status))
return nil;
return [NSString stringWithUTF8String: buffer];
#else
return nil; // FIXME Check
// msdn.microsoft.com/en-us/library/0h88fahh%28v=vs.85%29.aspx
#endif
}
+ (uint32_t) windowsLocaleCodeFromLocaleIdentifier: (NSString *)localeIdentifier
{
#if GS_USE_ICU == 1
return uloc_getLCID ([localeIdentifier UTF8String]);
#else
return 0; // FIXME: Check
// msdn.microsoft.com/en-us/library/0h88fahh%28v=vs.85%29.aspx
#endif
}
- (NSString *) displayNameForKey: (NSString *) key value: (id) value
{
#if GS_USE_ICU == 1
int32_t length = 0;
unichar buffer[ULOC_FULLNAME_CAPACITY];
UErrorCode status = 0;
const char *keyword = NULL;
const char *locale = [_localeId UTF8String];
if ([key isEqualToString: NSLocaleIdentifier])
{
length = uloc_getDisplayName([value UTF8String], locale,
(UChar *)buffer, sizeof(buffer)/sizeof(unichar),
&status);
}
else if ([key isEqualToString: NSLocaleLanguageCode])
{
length = uloc_getDisplayLanguage([value UTF8String], locale,
(UChar *)buffer, sizeof(buffer)/sizeof(unichar),
&status);
}
else if ([key isEqualToString: NSLocaleCountryCode])
{
length = uloc_getDisplayCountry([value UTF8String], locale,
(UChar *)buffer, sizeof(buffer)/sizeof(unichar),
&status);
}
else if ([key isEqualToString: NSLocaleScriptCode])
{
length = uloc_getDisplayCountry([value UTF8String], locale,
(UChar *)buffer, sizeof(buffer)/sizeof(unichar),
&status);
}
else if ([key isEqualToString: NSLocaleVariantCode])
{
length = uloc_getDisplayVariant([value UTF8String], locale,
(UChar *)buffer, sizeof(buffer)/sizeof(unichar),
&status);
}
else if ([key isEqualToString: NSLocaleCalendar])
{
keyword = ICUCalendarKeyword;
}
else if ([key isEqualToString: NSLocaleCollationIdentifier])
{
keyword = ICUCollationKeyword;
}
else
{
return nil;
}
/*
* TODO: Implement handling of the other locale component constants.
*/
if (NULL != keyword)
{
length = uloc_getDisplayKeywordValue ([value UTF8String], keyword,
locale, (UChar *)buffer, sizeof(buffer)/sizeof(unichar),
&status);
}
if (U_FAILURE(status))
return nil;
return [NSString stringWithCharacters: buffer length: (NSUInteger)length];
#else
return nil; // FIXME
#endif
}
- (id) initWithLocaleIdentifier: (NSString*)string
{
NSLocale *newLocale;
NSString *localeId;
#if GS_USE_ICU == 1
char cLocaleId[ULOC_FULLNAME_CAPACITY];
UErrorCode error = U_ZERO_ERROR;
localeId = [NSLocale canonicalLocaleIdentifierFromString: string];
// Normalize locale ID
uloc_canonicalize ([localeId UTF8String], cLocaleId,
ULOC_FULLNAME_CAPACITY, &error);
if (U_FAILURE(error))
{
[self release];
return nil;
}
localeId = [NSString stringWithUTF8String: cLocaleId];
#else
localeId = [NSLocale canonicalLocaleIdentifierFromString: string];
#endif
if (nil == localeId)
{
[self release];
return nil;
}
[classLock lock];
newLocale = [allLocales objectForKey: localeId];
if (nil == newLocale)
{
_localeId = [localeId copy];
_components = [[NSMutableDictionary alloc] initWithCapacity: 0];
[allLocales setObject: self forKey: localeId];
}
else
{
[self release];
self = [newLocale retain];
}
[classLock unlock];
return self;
}
- (NSString *) localeIdentifier
{
return _localeId;
}
- (id) objectForKey: (id) key
{
id result = nil;
#if GS_USE_ICU == 1
if (key == NSLocaleIdentifier || key == NSLocaleCollatorIdentifier)
return _localeId;
if ((result = [_components objectForKey: key]))
return result;
if ([_components count] == 0)
{
[_components addEntriesFromDictionary:
[NSLocale componentsFromLocaleIdentifier: _localeId]];
if ((result = [_components objectForKey: key]))
return result;
}
if ([key isEqualToString: NSLocaleUsesMetricSystem])
{
NSString *mSys = [_components objectForKey: key];
mSys = (mSys == nil) ? [self _getMeasurementSystem] : mSys;
if (mSys != nil)
{
[_components setValue: mSys forKey: NSLocaleMeasurementSystem];
if ([mSys isEqualToString: @"Metric"])
result = [NSNumber numberWithBool: YES];
else
result = [NSNumber numberWithBool: NO];
}
}
else if ([key isEqualToString: NSLocaleMeasurementSystem])
result = [self _getMeasurementSystem];
else if ([key isEqualToString: NSLocaleExemplarCharacterSet])
result = [self _getExemplarCharacterSet];
#if OS_API_VERSION(MAC_OS_X_VERSION_10_6, GS_API_LATEST)
else if ([key isEqualToString: NSLocaleQuotationBeginDelimiterKey])
result = [self _getDelimiterWithType: ULOCDATA_QUOTATION_START];
else if ([key isEqualToString: NSLocaleQuotationEndDelimiterKey])
result = [self _getDelimiterWithType: ULOCDATA_QUOTATION_END];
else if ([key isEqualToString: NSLocaleAlternateQuotationBeginDelimiterKey])
result = [self _getDelimiterWithType: ULOCDATA_ALT_QUOTATION_START];
else if ([key isEqualToString: NSLocaleAlternateQuotationEndDelimiterKey])
result = [self _getDelimiterWithType: ULOCDATA_ALT_QUOTATION_END];
#endif
else if ([key isEqualToString: NSLocaleCalendar])
result = [self _getCalendar];
else if ([key isEqualToString: NSLocaleDecimalSeparator])
result = [self _getDecimalSeparator];
else if ([key isEqualToString: NSLocaleGroupingSeparator])
result = [self _getGroupingSeparator];
else if ([key isEqualToString: NSLocaleCurrencySymbol])
result = [self _getCurrencySymbol];
else if ([key isEqualToString: NSLocaleCurrencyCode])
result = [self _getCurrencyCode];
[_components setValue: result forKey: key];
#endif
return result;
}
- (NSString *) languageCode
{
return [self objectForKey: NSLocaleLanguageCode];
}
- (NSString *) countryCode
{
return [self objectForKey: NSLocaleLanguageCode];
}
- (NSString *) scriptCode
{
return [self objectForKey: NSLocaleScriptCode];
}
- (NSString *) variantCode
{
return [self objectForKey: NSLocaleVariantCode];
}
- (NSCharacterSet *) exemplarCharacterSet
{
return [self objectForKey: NSLocaleExemplarCharacterSet];
}
- (NSString *) collationIdentifier
{
return [self objectForKey: NSLocaleCollationIdentifier];
}
- (NSString *) collatorIdentifier
{
return [self objectForKey: NSLocaleCollatorIdentifier];
}
- (NSString *) description
{
return _localeId;
}
- (BOOL) isEqual: (id)obj
{
if ([obj isKindOfClass: [self class]])
{
return [_localeId isEqual: [obj localeIdentifier]];
}
return NO;
}
- (void) dealloc
{
RELEASE(_localeId);
RELEASE(_components);
[super dealloc];
}
- (void) encodeWithCoder: (NSCoder*)encoder
{
[encoder encodeObject: _localeId];
}
- (id) initWithCoder: (NSCoder*)decoder
{
NSString *s = [decoder decodeObject];
return [self initWithLocaleIdentifier: s];
}
- (id) copyWithZone: (NSZone *) zone
{
NSLocale *result;
if (NSShouldRetainWithZone(self, zone))
result = RETAIN(self);
else
{
result = (NSLocale *)NSCopyObject(self, 0, zone);
result->_localeId = [_localeId copyWithZone: zone];
}
return result;
}
@end
@implementation NSLocale (PrimateMethods)
+ (void) _updateCanonicalLocales
{
NSBundle *gbundle = [NSBundle bundleForLibrary: @"gnustep-base"];
NSString *file = [gbundle pathForResource: @"Locale"
ofType: @"canonical"
inDirectory: @"Languages"];
if (file != nil)
canonicalLocales = [[NSDictionary alloc] initWithContentsOfFile: file];
}
- (NSString *) _getMeasurementSystem
{
#if GS_USE_ICU == 1
const char *cLocaleId;
ULocaleData *localeData;
UMeasurementSystem msystem;
UErrorCode err = U_ZERO_ERROR;
NSString *result = nil;
cLocaleId = [_localeId UTF8String];
localeData = ulocdata_open (cLocaleId, &err);
if (U_FAILURE(err))
return nil;
msystem = ulocdata_getMeasurementSystem (cLocaleId, &err);
if (U_SUCCESS(err))
{
if (msystem == UMS_SI)
result = @"Metric";
else
result = @"U.S.";
}
ulocdata_close (localeData);
return result;
#else
return nil;
#endif
}
- (NSCharacterSet *) _getExemplarCharacterSet
{
#if GS_USE_ICU == 1
const char *cLocaleId;
int idx;
int count;
UChar buffer[1024];
// This is an arbitrary size, increase it if it's not enough.
ULocaleData *localeData;
USet *charSet;
UErrorCode err = U_ZERO_ERROR;
NSCharacterSet *result;
NSMutableCharacterSet *mSet;
cLocaleId = [_localeId UTF8String];
localeData = ulocdata_open(cLocaleId, &err);
if (U_FAILURE(err))
{
return nil;
}
charSet = ulocdata_getExemplarSet(localeData, NULL,
USET_ADD_CASE_MAPPINGS, ULOCDATA_ES_STANDARD, &err);
if (U_FAILURE(err))
{
ulocdata_close(localeData);
return nil;
}
ulocdata_close(localeData);
mSet = [[NSMutableCharacterSet alloc] init];
if (mSet == nil)
{
uset_close(charSet);
return nil;
}
count = uset_getItemCount(charSet);
for (idx = 0 ; idx < count ; ++idx)
{
UChar32 start, end;
int strLen;
err = U_ZERO_ERROR;
strLen = uset_getItem(charSet, idx, &start, &end, buffer, 1024, &err);
if (U_FAILURE(err))
{
uset_close(charSet);
RELEASE(mSet);
return nil;
}
if (strLen == 0)
{
[mSet addCharactersInRange: NSMakeRange(start, (end - start) + 1)];
}
else if (strLen >= 2)
{
NSString *str = [NSString stringWithCharacters: buffer
length: strLen];
[mSet addCharactersInString: str];
}
// FIXME: The icu docs are a bit iffy and don't explain what len == 1
// means. So, if it is encountered, we simply skip it.
}