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

Add CI job to validate localized strings files #201

Merged
merged 1 commit into from
Jan 26, 2024
Merged
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
10 changes: 9 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ jobs:
runs-on: macOS-13
steps:
- name: Checkout Repo
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Build
run: bazel build //...
validate-strings:
name: Validate Localized Strings
runs-on: macOS-13
steps:
- name: Checkout Repo
uses: actions/checkout@v3
- name: Validate Localized Strings
run: Scripts/ValidateLocalizedStrings.swift
77 changes: 77 additions & 0 deletions Scripts/ValidateLocalizedStrings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/swift

import Foundation
import RegexBuilder

let fileManager = FileManager.default

guard let enumerator = fileManager.enumerator(
atPath: (fileManager.currentDirectoryPath as NSString)
.appendingPathComponent("Sources")
) else {
exit(1)
}

var swiftFiles: [String] = []
var stringsFiles: [String] = []

for filePath in enumerator {
let filePath = filePath as! String
if filePath.hasSuffix(".swift") {
swiftFiles.append("Sources/\(filePath)")
} else if filePath.hasSuffix(".strings") {
stringsFiles.append("Sources/\(filePath)")
}
}

print("Found source files:")
swiftFiles.forEach { print(" \($0)")}

print("Found strings files:")
stringsFiles.forEach { print(" \($0)")}

let localizedStringRegex = Regex {
"localized("
ZeroOrMore(.whitespace)
"key:"
ZeroOrMore(.whitespace)
"\""
Capture {
OneOrMore(.any.subtracting(.anyOf("\"")))
}
"\""
}

let localizedKeys = try swiftFiles.flatMap { filePath in
return try String(contentsOfFile: filePath)
.matches(of: localizedStringRegex)
.map { $0.1 }
}

let localizedKeysSet = Set(localizedKeys)

if localizedKeysSet.count != localizedKeys.count {
print("❌ There is a repeated localized string key")
exit(1)
}

let translationRegex = Regex {
Anchor.startOfLine
"\""
Capture {
OneOrMore(.any.subtracting(.anyOf("\"")))
}
}

var allValid = true

for filePath in stringsFiles {
let translationKeys = try String(contentsOfFile: filePath).matches(of: translationRegex).map { $0.1 }

if Set(translationKeys) != localizedKeysSet {
print("❌ \(filePath) does not match expected set of localized string keys")
allValid = false
}
}

exit(allValid ? 0 : 1)
Loading