Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger key_missing_from_base for iOS #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Sources/LocheckLogic/Validators/parseAndValidateXCStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,22 @@ func validateStrings(
path: baseString.path,
lineNumber: baseString.line)
}

let baseStringsKeys = Set(baseStrings.map(\.key))

var missingInBaseKeys: Set<String> = []
for translationString in translationStrings where !baseStringsKeys.contains(translationString.key) {
problemReporter.report(
KeyMissingFromBase(
key: translationString.key),
path: translationString.path,
lineNumber: translationString.line)
missingInBaseKeys.insert(translationString.key)
}

// MARK: Validate arguments

for translationString in translationStrings {
for translationString in translationStrings where !missingInBaseKeys.contains(translationString.key) {
let baseArgumentPositions = Set(translationString.base.arguments.map(\.position))
let translationArgumentPositions = Set(translationString.translation.arguments.map(\.position))

Expand Down
35 changes: 35 additions & 0 deletions Tests/LocheckLogicTests/ValidateStringsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,39 @@ class ValidateStringsTests: XCTestCase {
"abc:1: warning: 'missing' is missing from trnsltn (key_missing_from_translation)",
])
}

func testExtraKeyInTranslation() {
let problemReporter = ProblemReporter(log: false)
validateStrings(
baseStrings: [
LocalizedStringPair(
string: "\"present\" = \"present\";",
path: "abc",
line: 0,
basePath: "",
baseLineFallback: 0)!,
],
translationStrings: [
LocalizedStringPair(
string: "\"present\" = \"tneserp\";",
path: "def",
line: 0,
basePath: "",
baseLineFallback: 0)!,
LocalizedStringPair(
string: "\"extra\" = \"%s\";",
path: "abc",
line: 1,
basePath: "",
baseLineFallback: 0)!,
],
baseLanguageName: "en",
translationLanguageName: "trnsltn",
problemReporter: problemReporter)

XCTAssertEqual(
problemReporter.problems.map(\.messageForXcode), [
"abc:1: warning: 'extra' is missing from the base translation (key_missing_from_base)",
])
}
}