-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLDRDatabase.j
More file actions
131 lines (111 loc) · 3.76 KB
/
CLDRDatabase.j
File metadata and controls
131 lines (111 loc) · 3.76 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
/*
* CLDRDatabase.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/Foundation.j>
@import "CPDictionary+DeepMerge.j"
var SharedDatabase;
@implementation CLDRDatabase : CPObject
{
CPDictionary _cldrData;
}
// Return the shared instances
+ (CLDRDatabase)sharedDatabase
{
if (!SharedDatabase)
SharedDatabase = [[self alloc] init];
return SharedDatabase
}
+ (void)reset
{
SharedDatabase = nil;
}
- (id)init
{
self = [super init];
if (self)
{
_cldrData = [self _loadInitial];
}
return self;
}
// Load all locales for a specific language
- (CPDictionary)_loadLanguage:(CPString)languageIdentifier
{
if (_cldrData && ![[self availableLocaleIdentifiers] containsObject:languageIdentifier] && languageIdentifier != @"initial")
[CPException raise:@"UnknownLanguage" format:@"CLDRKit does not contain the language \"%@\"",languageIdentifier];
var url = [[CLDRKit bundle] pathForResource:[CPString stringWithFormat:"locales/%@.plist",languageIdentifier]],
data = [CPURLConnection sendSynchronousRequest:[CPURLRequest requestWithURL:url] returningResponse:nil];
return [data plistObject];
}
// Load the initial root (pre-merged) locale
- (CPDictionary)_loadInitial
{
return [self _loadLanguage:@"initial"];
}
- (void)_mergeLanguage:(CPString)languageIdentifier
{
var languageData = [self _loadLanguage: languageIdentifier];
[[[languageData objectForKey:@"locales"] allKeys] enumerateObjectsUsingBlock:(function (localeIdentifier){
[[_cldrData objectForKey:@"locales"] setObject:[[languageData objectForKey:@"locales"] objectForKey:localeIdentifier] forKey:localeIdentifier];
})];
}
// Locales currently loaded - this list may grow over time!
- (CPArray)loadedLocaleIdentifiers
{
return [[_cldrData valueForKeyPath:@"locales"] allKeys];
}
// Return an array of available locale identifiers. This includes all the root/pre-merged as well as possibly on-demand loaded locales
- (CPArray)availableLocaleIdentifiers
{
return [_cldrData valueForKeyPath:@"available"];
}
- (CPArray)countries
{
return [_cldrData valueForKey:@"countries"];
}
- (CPArray)countryCodes
{
return [[self countries] allKeys];
}
- (CPArray)languageCodes
{
return [_cldrData valueForKey:@"languages"];
}
- (CPDictionary)mergedLocaleWithIdentifier:(CPString)localeIdentifier
{
if (![[_cldrData objectForKey:@"main"] containsKey:localeIdentifier])
{
var language = localeIdentifier.split(/[-_]/)[0];
[self _mergeLanguage:language];
}
var merged = [[_cldrData objectForKey:@"locales"] objectForKey:@"root"],
partialIdentifier = @"";
[(localeIdentifier.split(/[-_]/)) enumerateObjectsUsingBlock:(function (localeIdentifierPart){
if ([partialIdentifier isEqualToString:@""])
partialIdentifier = localeIdentifierPart;
else
partialIdentifier = [partialIdentifier stringByAppendingFormat:@"_%@",localeIdentifierPart];
merged = [merged dictionaryByDeepMergingObjectsFromDictionary:[[_cldrData objectForKey:@"locales"] objectForKey:partialIdentifier]];
})];
return merged;
}
@end