Skip to content

Commit 2d46f0c

Browse files
committed
Add redundance check
1 parent 06ce8a2 commit 2d46f0c

File tree

5 files changed

+58
-38
lines changed

5 files changed

+58
-38
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"IgnoredUntranslatedKey" = "OK";
22
"MissingKey" = "Hey I'm not present in Spanish!";
33
"NeverUsedKey" = "Hey I'm never used so you can just get rid of me!";
4-
"UntranslatedKey" = "Hey I'm always in english so you need to translate me!";
4+
"UntranslatedKey" = "Hey I'm always in english so you need to translate me!";
5+
"DuplicatedKey" = "Hey, I'm here more than once!";
6+
"DuplicatedKey" = "Hey, I'm here more than once!";
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
"IgnoredUntranslatedKey" = "OK"; //ignore-same-translation-warning
22
"NeverUsedKey" = "Hello I'm never used so you can just get rid of me!";
33
"UntranslatedKey" = "Hey I'm always in english so you need to translate me!";
4+
"DuplicatedKey" = "Hey, I'm more than once in the master translations!";
5+
"RedundantKey" = "Hi, I'm a redundant key only appearing in Spanish";
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"IgnoredUntranslatedKey" = "OK"; //ignore-same-translation-warning
2-
"MissingKey" = "Hey! je n'existe pas en espagnol !";
2+
"MissingKey" = "Hey! je n'existe pas en espagnol!";
33
"NeverUsedKey" = "Bonjour I'm never used so you can just get rid of me!";
4-
"UntranslatedKey" = "Hey I'm always in english so you need to translate me!";
4+
"DuplicatedKey" = "Hey, I'm more than once in the master translations!";
5+
"UntranslatedKey" = "Hey I'm always in english so you need to translate me!";

Example/LocalizeExample/Sources/ViewController.swift

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ final class ViewController: UIViewController {
1616
_ = NSLocalizedString("UntranslatedKey", comment: "")
1717
_ = NSLocalizedString("IgnoredUntranslatedKey", comment: "")
1818
_ = NSLocalizedString("MissingKey", comment: "")
19+
_ = NSLocalizedString("DuplicatedKey", comment: "")
1920
_ = NSLocalizedString("MissingKeyFromMain", comment: "This key is not present in master translation file")
2021
}
2122
}

Localize.swift

100644100755
+49-35
Original file line numberDiff line numberDiff line change
@@ -132,44 +132,49 @@ struct LocalizationFiles {
132132
sortLinesAlphabetically()
133133
}
134134
let location = singleLanguage ? "\(path)/Localizable.strings" : "\(path)/\(name).lproj/Localizable.strings"
135-
if let string = try? String(contentsOfFile: location, encoding: .utf8) {
136-
let lines = string.components(separatedBy: .newlines)
137-
keyValue = [:]
138-
let pattern = "\"(.*)\" = \"(.+)\";"
139-
let regex = try? NSRegularExpression(pattern: pattern, options: [])
140-
var ignoredTranslation: [String] = []
141-
142-
for (lineNumber, line) in lines.enumerated() {
143-
let range = NSRange(location: 0, length: (line as NSString).length)
144-
145-
// Ignored pattern
146-
let ignoredPattern = "\"(.*)\" = \"(.+)\"; *\\/\\/ *ignore-same-translation-warning"
147-
let ignoredRegex = try? NSRegularExpression(pattern: ignoredPattern, options: [])
148-
if let ignoredMatch = ignoredRegex?.firstMatch(in: line,
149-
options: [],
150-
range: range) {
151-
let key = (line as NSString).substring(with: ignoredMatch.range(at: 1))
152-
ignoredTranslation.append(key)
153-
}
154-
if let firstMatch = regex?.firstMatch(in: line, options: [], range: range) {
155-
let key = (line as NSString).substring(with: firstMatch.range(at: 1))
156-
let value = (line as NSString).substring(with: firstMatch.range(at: 2))
157-
if let _ = keyValue[key] {
158-
let str = "\(path)/\(name).lproj"
159-
+ "/Localizable.strings:\(linesNumbers[key]!): "
160-
+ "error: [Redundance] \"\(key)\" "
161-
+ "is redundant in \(name.uppercased()) file"
162-
print(str)
163-
numberOfErrors += 1
164-
} else {
165-
keyValue[key] = value
166-
linesNumbers[key] = lineNumber + 1
167-
}
135+
guard let string = try? String(contentsOfFile: location, encoding: .utf8) else {
136+
return
137+
}
138+
139+
let lines = string.components(separatedBy: .newlines)
140+
keyValue = [:]
141+
142+
let pattern = "\"(.*)\" = \"(.+)\";"
143+
let regex = try? NSRegularExpression(pattern: pattern, options: [])
144+
var ignoredTranslation: [String] = []
145+
146+
for (lineNumber, line) in lines.enumerated() {
147+
let range = NSRange(location: 0, length: (line as NSString).length)
148+
149+
// Ignored pattern
150+
let ignoredPattern = "\"(.*)\" = \"(.+)\"; *\\/\\/ *ignore-same-translation-warning"
151+
let ignoredRegex = try? NSRegularExpression(pattern: ignoredPattern, options: [])
152+
if let ignoredMatch = ignoredRegex?.firstMatch(in: line,
153+
options: [],
154+
range: range) {
155+
let key = (line as NSString).substring(with: ignoredMatch.range(at: 1))
156+
ignoredTranslation.append(key)
157+
}
158+
159+
if let firstMatch = regex?.firstMatch(in: line, options: [], range: range) {
160+
let key = (line as NSString).substring(with: firstMatch.range(at: 1))
161+
let value = (line as NSString).substring(with: firstMatch.range(at: 2))
162+
163+
if keyValue[key] != nil {
164+
let str = "\(path)/\(name).lproj"
165+
+ "/Localizable.strings:\(linesNumbers[key]!): "
166+
+ "error: [Duplication] \"\(key)\" "
167+
+ "is duplicated in \(name.uppercased()) file"
168+
print(str)
169+
numberOfErrors += 1
170+
} else {
171+
keyValue[key] = value
172+
linesNumbers[key] = lineNumber + 1
168173
}
169174
}
170-
print(ignoredFromSameTranslation)
171-
ignoredFromSameTranslation[name] = ignoredTranslation
172175
}
176+
print(ignoredFromSameTranslation)
177+
ignoredFromSameTranslation[name] = ignoredTranslation
173178
}
174179

175180
func rebuildFileString(from lines: [String]) -> String {
@@ -306,6 +311,15 @@ for file in localizationFiles {
306311
numberOfErrors += 1
307312
}
308313
}
314+
315+
let redundantKeys = file.keyValue.keys.filter { !masterLocalizationFile.keyValue.keys.contains($0) }
316+
317+
for k in redundantKeys {
318+
let str = "\(path)/\(file.name).lproj/Localizable.strings:\(file.linesNumbers[k]!): "
319+
+ "error: [Redundant key] \"\(k)\" redundant in \(file.name.uppercased()) file"
320+
321+
print(str)
322+
}
309323
}
310324

311325
if checkForUntranslated {

0 commit comments

Comments
 (0)