-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPLocale+CLDRKit.j
More file actions
132 lines (103 loc) · 4.42 KB
/
Copy pathCPLocale+CLDRKit.j
File metadata and controls
132 lines (103 loc) · 4.42 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
/*
* CPLocale+CLDRKit.j
* CLDRKit
*
* Created by Udo Schneider on January 1, 2014.
*
* Copyright 2014, Krodelin Software Solutions. All rights reserved.
*
* 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.1 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; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/CPLocale.j>
@implementation CPLocale (CLDRKit)
+ (id)localeWithLocaleIdentifier:(CPString)string
{
return [[CPLocale alloc] initWithLocaleIdentifier:string];
}
+ (CPArray)availableLocaleIdentifiers
{
return [[CLDRDatabase sharedDatabase] availableLocaleIdentifiers];
}
+ (CPArray)ISOLanguageCodes
{
return [[CLDRDatabase sharedDatabase] languageCodes];
}
+ (CPArray)ISOCountryCodes
{
return [[CLDRDatabase sharedDatabase] countryCodes];
}
+ (CPDictionary)componentsFromLocaleIdentifier:(CPString)aLocaleIdentifier
{
var localIdentifierParts = aLocaleIdentifier.split(/[-_]/),
components = [CPMutableDictionary dictionary],
language = localIdentifierParts.shift();
if (!language)
[CPException raise:@"InvalidLocaleIdentifier" format:@"CLDRKit could not parse the locale identifier \"%@\"", aLocaleIdentifier];
[components setObject:aLocaleIdentifier forKey:CPLocaleIdentifier];
[components setObject:language forKey:CPLocaleLanguageCode];
var territoryOrScript = localIdentifierParts.shift();
if (!territoryOrScript)
return components;
if (territoryOrScript.length == 4)
{
[components setObject:territoryOrScript forKey:CPLocaleScriptCode];
territory = localIdentifierParts.shift();
}
else
territory = territoryOrScript;
if (!territory)
return components;
[components setObject:territory forKey:CPLocaleCountryCode];
var variant = localIdentifierParts.join("_");
if (variant)
[components setObject:variant forKey:CPLocaleVariantCode];
return components;
}
+ (void)_platformLocaleAdditionalDescriptionForIdentifier:(CPString)aLocaleIdentifier
{
var additionalData = [CPMutableDictionary dictionary],
cldrData = [[CLDRDatabase sharedDatabase] mergedLocaleWithIdentifier:aLocaleIdentifier],
components = [self componentsFromLocaleIdentifier:aLocaleIdentifier];
[additionalData addEntriesFromDictionary:cldrData];
[additionalData addEntriesFromDictionary:components];
// CPLocaleExemplarCharacterSet
[additionalData setObject:[CPCharacterSet characterSetWithCharactersInString:[cldrData valueForKeyPath:@"_CPLocaleExemplarCharacterSetString"]] forKey:CPLocaleExemplarCharacterSet];
var countries = [[CLDRDatabase sharedDatabase] countries],
country = [components objectForKey:CPLocaleCountryCode];
// CPLocaleCurrencySymbol
[additionalData setObject:[[countries objectForKey:country] objectForKey:CPLocaleCurrencySymbol] forKey:CPLocaleCurrencySymbol];
// CPLocaleCurrencyCode
[additionalData setObject:[[countries objectForKey:country] objectForKey:CPLocaleCurrencyCode] forKey:CPLocaleCurrencyCode];
// print([additionalData description]);
return additionalData;
}
- (CPString)displayNameForKey:(id)key value:(id)value
{
// TODO: cldrData can be cached on per-instance basis
var cldrData = [[CLDRDatabase sharedDatabase] mergedLocaleWithIdentifier:[self localeIdentifier]],
localeDisplayNames = [cldrData objectForKey:@"_localeDisplayNames"],
localeComponents = [CPLocale componentsFromLocaleIdentifier:value],
languageComponent = [localeComponents objectForKey:CPLocaleLanguageCode],
language = [[localeDisplayNames objectForKey:CPLocaleLanguageCode] objectForKey:languageComponent],
countryComponent = [localeComponents objectForKey:CPLocaleCountryCode],
country = [[localeDisplayNames objectForKey:CPLocaleCountryCode] objectForKey:countryComponent];
if (key == CPLocaleLanguageCode)
return language;
if (key == CPLocaleCountryCode)
return country;
if (key == CPLocaleIdentifier)
return [CPString stringWithFormat:[localeDisplayNames objectForKey:@"localePattern"],language,country];
}
@end