-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add localization tests to guarantee all localized keys have translati…
…ons in all supported localizations
- Loading branch information
1 parent
88bdab3
commit cc73b5a
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
EssentialFeed/EssentialFeediOSTests/Feed Presentation/FeedLocalizationTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// FeedLocalizationTests.swift | ||
// EssentialFeediOSTests | ||
// | ||
// Created by Oluwaseun Adebanwo on 29/11/2023. | ||
// | ||
|
||
import XCTest | ||
@testable import EssentialFeediOS | ||
|
||
final class FeedLocalizationTests: XCTestCase { | ||
|
||
func test_localizedStrings_haveKeysAndValuesForAllSupportedLocalizations() { | ||
let table = "Feed" | ||
let presentationBundle = Bundle(for: FeedPresenter.self) | ||
let localizationBundles = allLocalizationBundles(in: presentationBundle) | ||
let localizedStringKeys = allLocalizedStringKeys(in: localizationBundles, table: table) | ||
|
||
localizationBundles.forEach { (bundle, localization) in | ||
localizedStringKeys.forEach { key in | ||
let localizedString = bundle.localizedString(forKey: key, value: nil, table: table) | ||
|
||
if localizedString == key { | ||
let language = Locale.current.localizedString(forLanguageCode: localization) ?? "" | ||
|
||
XCTFail("Missing \(language) (\(localization)) localized string for key: '\(key)' in table: '\(table)'") | ||
} | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Helpers | ||
|
||
private typealias LocalizedBundle = (bundle: Bundle, localization: String) | ||
|
||
private func allLocalizationBundles(in bundle: Bundle, file: StaticString = #file, line: UInt = #line) -> [LocalizedBundle] { | ||
return bundle.localizations.compactMap { localization in | ||
guard | ||
let path = bundle.path(forResource: localization, ofType: "lproj"), | ||
let localizedBundle = Bundle(path: path) | ||
else { | ||
XCTFail("Couldn't find bundle for localization: \(localization)", file: file, line: line) | ||
return nil | ||
} | ||
|
||
return (localizedBundle, localization) | ||
} | ||
} | ||
|
||
private func allLocalizedStringKeys(in bundles: [LocalizedBundle], table: String, file: StaticString = #file, line: UInt = #line) -> Set<String> { | ||
return bundles.reduce([]) { (acc, current) in | ||
guard | ||
let path = current.bundle.path(forResource: table, ofType: "strings"), | ||
let strings = NSDictionary(contentsOfFile: path), | ||
let keys = strings.allKeys as? [String] | ||
else { | ||
XCTFail("Couldn't load localized strings for localization: \(current.localization)", file: file, line: line) | ||
return acc | ||
} | ||
|
||
return acc.union(Set(keys)) | ||
} | ||
} | ||
} |