Skip to content

Commit

Permalink
Add check for untranslated
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsrutek committed Apr 5, 2020
1 parent 900f439 commit 06ce8a2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Example/LocalizeExample/Sources/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

import UIKit

class ViewController: UIViewController {
final class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

// Here simulates their usage in code
_ = NSLocalizedString("UntranslatedKey", comment: "")
_ = NSLocalizedString("IgnoredUntranslatedKey", comment: "")
_ = NSLocalizedString("MissingKey", comment: "")
_ = NSLocalizedString("MissingKeyFromMain", comment: "This key is not present in master translation file")
}
}
29 changes: 23 additions & 6 deletions Localize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ let sanitizeFiles = false
*/
let singleLanguage = false

/*
Determines if we should show errors if there's a key within the app
that does not appear in master translations.
*/
let checkForUntranslated = true

// MARK: End Of Configurable Section
// MARK: -

Expand Down Expand Up @@ -219,7 +225,7 @@ struct LocalizationFiles {

// MARK: - Load Localisation Files in memory

let masterLocalizationfile = LocalizationFiles(name: masterLanguage)
let masterLocalizationFile = LocalizationFiles(name: masterLanguage)
let localizationFiles = supportedLanguages
.filter { $0 != masterLanguage }
.map { LocalizationFiles(name: $0) }
Expand Down Expand Up @@ -252,16 +258,17 @@ while let swiftFileLocation = enumerator?.nextObject() as? String {
}
}

var masterKeys = Set(masterLocalizationfile.keyValue.keys)
var masterKeys = Set(masterLocalizationFile.keyValue.keys)
let usedKeys = Set(localizedStrings)
let ignored = Set(ignoredFromUnusedKeys)
let unused = masterKeys.subtracting(usedKeys).subtracting(ignored)
let untranslated = usedKeys.subtracting(masterKeys)

// Here generate Xcode regex Find and replace script to remove dead keys all at once!
var replaceCommand = "\"("
var counter = 0
for v in unused {
var str = "\(path)/\(masterLocalizationfile.name).lproj/Localizable.strings:\(masterLocalizationfile.linesNumbers[v]!): "
var str = "\(path)/\(masterLocalizationFile.name).lproj/Localizable.strings:\(masterLocalizationFile.linesNumbers[v]!): "
str += "error: [Unused Key] \"\(v)\" is never used"
print(str)
numberOfErrors += 1
Expand All @@ -280,9 +287,9 @@ print(replaceCommand)
// MARK: - Compare each translation file against master (en)

for file in localizationFiles {
for k in masterLocalizationfile.keyValue.keys {
for k in masterLocalizationFile.keyValue.keys {
if let v = file.keyValue[k] {
if v == masterLocalizationfile.keyValue[k] {
if v == masterLocalizationFile.keyValue[k] {
if !ignoredFromSameTranslation[file.name]!.contains(k) {
let str = "\(path)/\(file.name).lproj/Localizable.strings"
+ ":\(file.linesNumbers[k]!): "
Expand All @@ -293,14 +300,24 @@ for file in localizationFiles {
}
}
} else {
var str = "\(path)/\(file.name).lproj/Localizable.strings:\(masterLocalizationfile.linesNumbers[k]!): "
var str = "\(path)/\(file.name).lproj/Localizable.strings:\(masterLocalizationFile.linesNumbers[k]!): "
str += "error: [Missing] \"\(k)\" missing from \(file.name.uppercased()) file"
print(str)
numberOfErrors += 1
}
}
}

if checkForUntranslated {
for key in untranslated {
var str = "\(path)/\(masterLocalizationFile.name).lproj/Localizable.strings:1: "
str += "error: [Missing Translation] \(key) is not translated"

print(str)
numberOfErrors += 1
}
}

print("Number of warnings : \(numberOfWarnings)")
print("Number of errors : \(numberOfErrors)")

Expand Down

0 comments on commit 06ce8a2

Please sign in to comment.