In my App I\'m using NSLocalizedString
to localize my app. Now I want to switch to UITests
and habe Testcode like this:
[tabBarsQue
The answer of SeanR is great (+1), but there is a minor improvement:
If you use base localization, then your Localizable.strings
might not be localized in your base language. This is not necessary because the base language would be used in this case. If so, SeanR’s function localizedString
would return „?“
.
The extended version below checks additionally for the base language, and returns the localized string in the base language:
func localizedString(_ key: String) -> String {
let testBundle = Bundle(for: ShopEasyUITests.self)
guard let currentLanguage = currentLanguage else { return "?" }
if let testBundlePath = testBundle.path(forResource: currentLanguage.localeCode, ofType: "lproj"),
let localizedBundle = Bundle(path: testBundlePath) {
return NSLocalizedString(key, bundle: localizedBundle, comment: "")
}
if let testBundlePath = testBundle.path(forResource: currentLanguage.langCode, ofType: "lproj"),
let localizedBundle = Bundle(path: testBundlePath) {
return NSLocalizedString(key, bundle: localizedBundle, comment: "")
}
if let testBundlePath = testBundle.path(forResource: "Base", ofType: "lproj"),
let localizedBundle = Bundle(path: testBundlePath) {
return NSLocalizedString(key, bundle: localizedBundle, comment: "")
}
return "?"
}