Skip to content

Commit

Permalink
codegen: Handle string catalogue files (WIP). #49
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeyl committed Jun 24, 2024
1 parent 86d9229 commit 42167a1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Sources/Shark/CLI/XcodeProjectHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,15 @@ struct XcodeProjectHelper {
.compactMap { $0.file }
.flatMap(paths(for:))
.reduce(into: ResourcePaths(), { result, path in
//print("Dealing with \(path)...")
if !self.options.shouldExclude(path: path) {
switch path.pathExtension {
case "xcassets":
result.assetsPaths.append(path)
case "strings" where path.pathComponents.contains("\(locale).lproj"):
result.localizationPaths.append(path)
case "xcstrings":
result.localizationPaths.append(path)
case "ttf", "otf", "ttc":
result.fontPaths.append(path)
case "storyboard":
Expand Down
31 changes: 28 additions & 3 deletions Sources/Shark/Codegen/LocalizationEnumBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,40 @@ enum LocalizationBuilderError: LocalizedError {
}

enum LocalizationEnumBuilder {

static func localizationsEnumString(forFilesAtPaths paths: [String], topLevelName: String, options: Options) throws -> String? {
let termsDictionaries = try paths.compactMap({ path -> [String: String]? in
guard FileManager.default.fileExists(atPath: path) else { return nil }

let paths = paths.filter { FileManager.default.fileExists(atPath: $0) }
// We now support both `.strings` and `.xcstrings` files.
let stringsPaths = paths.filter { $0.hasSuffix(".strings") }
let xcstringsPaths = paths.filter { $0.hasSuffix(".xcstrings") }

let stringsDictionaries = try stringsPaths.compactMap { path -> [String: String]? in
guard let termsDictionary = NSDictionary(contentsOfFile: path) as? [String: String] else {
throw LocalizationBuilderError.invalidLocalizableStringsFile(path: path)
}
return termsDictionary
})
}

let xcstringsDictionaries = try xcstringsPaths.compactMap { path -> [String: String]? in
let url = URL(fileURLWithPath: path)
let fileContents = try Data(contentsOf: url)
let stringCatalog = try JSONDecoder().decode(StringCatalog.self, from: fileContents)

var terms: [String: String] = [:]
for (string, entry) in stringCatalog.strings {
guard let localizations = entry.localizations,
let sourceLocalization = localizations[stringCatalog.sourceLanguage],
let value = sourceLocalization.stringUnit?.value else {
terms[string] = string
continue
}
terms[string] = value
}
return terms
}

let termsDictionaries = stringsDictionaries + xcstringsDictionaries
guard termsDictionaries.isEmpty == false else { return nil }

let rootNode = Node(value: LocalizationValue.namespace(name: topLevelName))
Expand Down
44 changes: 44 additions & 0 deletions Sources/Shark/Types/StringCatalogModels.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
struct StringCatalog: Decodable {
let sourceLanguage: String
let strings: [String: StringCatalogEntry]
}

struct StringCatalogEntry: Decodable {
let comment: String?
let localizations: [String: Localization]?
}

struct Localization: Decodable {
let stringUnit: StringUnit?
let variations: Variations?
}

struct Variations: Decodable {
let plural: PluralVariation?
}

struct PluralVariation: Decodable {
let zero: Variation?
let one: Variation?
let two: Variation?
let few: Variation?
let many: Variation?
let other: Variation

var all: [Variation] { [zero, one, two, few, many, other].compactMap { $0 } }
}

struct Variation: Decodable {

enum TranslationState: Decodable {
case translated
case needs_review
}

let stringUnit: StringUnit
let state: TranslationState
}

struct StringUnit: Decodable {
let value: String
}

0 comments on commit 42167a1

Please sign in to comment.