mirror of
https://github.com/yonaskolb/XcodeGen.git
synced 2026-03-18 20:02:25 +00:00
Support for Strings Catalogs (Xcode 15) (#1421)
* Support for xcode 15 string catalogs * Add sample string catalog to Test Fixture and basic test to check that asset catalogs are added in the resources build phase * Restore unintended changes * Update Pull Request number for 'Support for Strings Catalogs' in changelog * Update fixture yml generator * Detect knownRegions based on locales in string catalogs
This commit is contained in:
@@ -0,0 +1,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)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user