forked from yonaskolb/XcodeGen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringCatalogDecoding.swift
More file actions
82 lines (67 loc) · 2.08 KB
/
StringCatalogDecoding.swift
File metadata and controls
82 lines (67 loc) · 2.08 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
import Foundation
import JSONUtilities
import PathKit
struct StringCatalog {
/**
* Sample string catalog:
* {
* "sourceLanguage" : "en",
* "strings" : {
* "foo" : {
* "localizations" : {
* "en" : {
* ...
* },
* "es" : {
* ...
* },
* "it" : {
* ...
* }
* }
* }
* }
* }
*/
private struct CatalogItem {
private enum JSONKeys: String {
case localizations
}
private let key: String
let locales: Set<String>
init?(key: String, from jsonDictionary: JSONDictionary) {
guard let localizations = jsonDictionary[JSONKeys.localizations.rawValue] as? JSONDictionary else {
return nil
}
self.key = key
self.locales = Set(localizations.keys)
}
}
private enum JSONKeys: String {
case strings
}
private let strings: [CatalogItem]
init?(from path: Path) {
guard let catalogDictionary = try? JSONDictionary.from(url: path.url),
let catalog = StringCatalog(from: catalogDictionary) else {
return nil
}
self = catalog
}
private init?(from jsonDictionary: JSONDictionary) {
guard let stringsDictionary = jsonDictionary[JSONKeys.strings.rawValue] as? JSONDictionary else {
return nil
}
self.strings = stringsDictionary.compactMap { key, value -> CatalogItem? in
guard let stringDictionary = value as? JSONDictionary else {
return nil
}
return CatalogItem(key: key, from: stringDictionary)
}
}
var includedLocales: Set<String> {
strings.reduce(Set<String>(), { partialResult, catalogItem in
partialResult.union(catalogItem.locales)
})
}
}