Skip to content

Instantly share code, notes, and snippets.

@Ben-G
Created June 16, 2017 20:35
Show Gist options
  • Save Ben-G/3cd80dbf43f40a769bc343597c8c664e to your computer and use it in GitHub Desktop.
Save Ben-G/3cd80dbf43f40a769bc343597c8c664e to your computer and use it in GitHub Desktop.
Very simple automated test to ensure localizations exist and are well-formed on iOS.
import Foundation
import XCTest
/// Basic sanity check that ensures that we are able to retrieve localized strings for all languages
/// we support.
final class L10NTests: XCTestCase {
func testLocalizations() {
let locales = ["en", "es", "zh-Hans", "zh-Hant", "fi"]
for locale in locales {
verify(localeIdentifier: locale)
}
}
}
extension L10NTests {
/// Verifies a localization is available by fetching one string from the Localizable.strings file.
func verify(localeIdentifier: String) {
guard let path = Bundle.main.path(forResource: localeIdentifier, ofType: "lproj"),
let bundle = Bundle(path: path) else {
XCTFail("Missing localization for \(localeIdentifier)"); return
}
// Pick any string from the Localizable.strings file; ideally one that's unlikely to be removed ;)
let string = bundle.localizedString(forKey: "generic.delete", value: nil, table: nil)
XCTAssertFalse(string.isEmpty)
XCTAssertNotEqual(string, "generic.delete")
}
}
@dderg
Copy link

dderg commented Aug 1, 2021

would be cool if it would test if all strings exist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment