Skip to content

Instantly share code, notes, and snippets.

@toddhopkinson
Forked from Ben-G/L10NTests.swift
Created June 16, 2017 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toddhopkinson/3797d5ee37c8d250dc82ea772b49a30f to your computer and use it in GitHub Desktop.
Save toddhopkinson/3797d5ee37c8d250dc82ea772b49a30f 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")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment