Skip to content

Commit 55c3d7e

Browse files
authored
Merge pull request #1 from popsa-hq/jaspervaldivia/Add-exceptions-to-strings-checker
Add exceptions to strings checker
2 parents a6be405 + bb0548d commit 55c3d7e

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

main.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ guard let enumerator = FileManager.default.enumerator(at: URL(fileURLWithPath: F
1616
var missingStrings: [String: [String]] = [:]
1717

1818
for case let url as URL in enumerator where try url.resourceValues(forKeys: [.isRegularFileKey]).isRegularFile == true {
19+
var exceptions = Set<String>()
20+
if url.pathExtension == "txt" && url.deletingPathExtension().lastPathComponent == "ignored_translation_keys" {
21+
exceptions = try parseExceptionKeys(at: url)
22+
}
1923
if url.pathExtension == "strings" {
20-
let emptyStrings = try parseEmptyStringsFile(at: url)
24+
let emptyStrings = try parseEmptyStringsFile(at: url, with: exceptions)
2125
if !emptyStrings.isEmpty {
2226
missingStrings[url.pathComponents[max(0, url.pathComponents.count - 2)], default: []].append(contentsOf: emptyStrings)
2327
}
2428
}
2529
if url.pathExtension == "stringsdict" {
26-
let emptyStrings = try parseEmptyStringsDictFile(at: url)
30+
let emptyStrings = try parseEmptyStringsDictFile(at: url, with: exceptions)
2731
if !emptyStrings.isEmpty {
2832
missingStrings[url.pathComponents[max(0, url.pathComponents.count - 2)], default: []].append(contentsOf: emptyStrings)
2933
}
@@ -41,21 +45,26 @@ if !missingStrings.isEmpty {
4145
print("No missing strings in project")
4246
}
4347

44-
func parseEmptyStringsFile(at url: URL) throws -> [String] {
48+
func parseExceptionKeys(at url: URL) throws -> Set<String> {
49+
let strings = try String(contentsOf: url).components(separatedBy: .newlines)
50+
return Set(strings)
51+
}
52+
53+
func parseEmptyStringsFile(at url: URL, with exceptions: Set<String>) throws -> [String] {
4554
let strings = try String(contentsOf: url).components(separatedBy: .newlines)
4655
let keyValues = strings.compactMap { string -> String? in
4756
var string = string
4857
guard string.popLast() == ";" else { return nil }
4958
let components = string.components(separatedBy: "=").map { $0.trimmingCharacters(in: .whitespaces).replacingOccurrences(of: "\"", with: "") }
5059
guard components.count == 2 else { return nil }
51-
guard !components[0].isEmpty else { return nil }
60+
guard !components[0].isEmpty, !exceptions.contains(components[0]) else { return nil }
5261
guard components[1].isEmpty else { return nil }
5362
return components[0]
5463
}
5564
return keyValues
5665
}
5766

58-
func parseEmptyStringsDictFile(at url: URL) throws -> [String] {
67+
func parseEmptyStringsDictFile(at url: URL, with exceptions: Set<String>) throws -> [String] {
5968
struct StringsDictionaryValue: Decodable {
6069
struct Format: Decodable {
6170
var NSStringFormatSpecTypeKey: String
@@ -78,7 +87,7 @@ func parseEmptyStringsDictFile(at url: URL) throws -> [String] {
7887
dictionary.format.many?.isEmpty != true,
7988
dictionary.format.few?.isEmpty != true
8089
else {
81-
return key
90+
return exceptions.contains(key) ? nil : key
8291
}
8392
return nil
8493
}

0 commit comments

Comments
 (0)